-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
ENH: reset_index on a MultiIndex with duplicate levels raises a ValueError #44755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
88cd26f
a23dbdc
b101177
39d9f75
c83b84b
e1a5910
e5ab5f7
b4828e6
d9d60ee
e1bb16f
e924f93
65c6fef
704fae4
2911dd7
96af74d
0e90ae9
170d05f
fe23eeb
a0f0e4c
01ad538
9da58ed
9d988a0
a108a70
13c6dce
2b37ab3
140b5d9
1bd1cbd
e27df82
749838c
c7cf483
7711474
0ecf6fd
0730d7b
99ea2c3
250999a
090468e
14a07e1
8eb8710
18341db
1337283
a8dab78
95cc65b
7de074c
bb54268
01136ae
d21846b
3ef667c
22deac4
540b307
8cc81b0
fc74265
a59644a
b774598
67a5956
e88b8e2
9ad3026
1fdc39f
7639464
c58d992
e07eea6
04b3a38
2f94170
9241f96
0b84426
c2bed8f
3530ca8
dddae07
14edbdb
8383572
37cc560
3625d77
9d0f798
e2fbf3e
0b2a9d1
c5618c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -328,13 +328,21 @@ def test_reset_index_multiindex_nan(self): | |
) | ||
def test_reset_index_with_datetimeindex_cols(self, name): | ||
# GH#5818 | ||
warn = None | ||
if isinstance(name, Timestamp) and name.tz is not None: | ||
# _deprecate_mismatched_indexing | ||
warn = FutureWarning | ||
|
||
df = DataFrame( | ||
[[1, 2], [3, 4]], | ||
columns=date_range("1/1/2013", "1/2/2013"), | ||
index=["A", "B"], | ||
) | ||
df.index.name = name | ||
result = df.reset_index() | ||
|
||
with tm.assert_produces_warning(warn): | ||
result = df.reset_index() | ||
|
||
item = name if name is not None else "index" | ||
columns = Index([item, datetime(2013, 1, 1), datetime(2013, 1, 2)]) | ||
if isinstance(item, str) and item == "2012-12-31": | ||
|
@@ -366,8 +374,13 @@ def test_reset_index_multiindex_columns(self): | |
result = df[["B"]].rename_axis("A").reset_index() | ||
tm.assert_frame_equal(result, df) | ||
|
||
# GH#16120: already existing column | ||
msg = r"cannot insert \('A', ''\), already exists" | ||
with pytest.raises(ValueError, match=msg): | ||
df.rename_axis("A").reset_index() | ||
|
||
# GH#44755 reset_index with duplicate column labels | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you make a separate test, also test with option_context and setting the option There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have made a separate test, and included tests of the DataFrame flag (which was tricky, because many things stop working if the flag is set to False! E.g. rename_axis()) |
||
result = df.rename_axis("A").reset_index() | ||
result = df.rename_axis("A").reset_index(allow_duplicates=True) | ||
levels = [["A", ""], ["A", ""], ["B", "b"]] | ||
expected = DataFrame( | ||
[[0, 0, 2], [1, 1, 3]], columns=MultiIndex.from_tuples(levels) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Micro optimization - can you move this outside the for loop? It's constant and doesn't need to be evaluated repeatedly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done