Skip to content

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

Merged
merged 75 commits into from
Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
88cd26f
reset_index to handle duplicate column labels
johnzangwill Dec 4, 2021
a23dbdc
Add tests
johnzangwill Dec 5, 2021
b101177
Add tests
johnzangwill Dec 5, 2021
39d9f75
Update v1.4.0.rst
johnzangwill Dec 5, 2021
c83b84b
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 5, 2021
e1a5910
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 5, 2021
e5ab5f7
Implement allow_duplicates parameter
johnzangwill Dec 6, 2021
b4828e6
Formatting
johnzangwill Dec 6, 2021
d9d60ee
Add docstrings
johnzangwill Dec 6, 2021
e1bb16f
Trigger CI
johnzangwill Dec 6, 2021
e924f93
Trigger CI
johnzangwill Dec 6, 2021
65c6fef
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 6, 2021
704fae4
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 6, 2021
2911dd7
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 7, 2021
96af74d
Update v1.4.0.rst
johnzangwill Dec 7, 2021
0e90ae9
Update test_reset_index.py
johnzangwill Dec 7, 2021
170d05f
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 8, 2021
fe23eeb
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 10, 2021
a0f0e4c
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 10, 2021
01ad538
Make separate allow_duplicates test
johnzangwill Dec 10, 2021
9da58ed
Update test_reset_index.py
johnzangwill Dec 10, 2021
9d988a0
Update test_reset_index.py
johnzangwill Dec 10, 2021
a108a70
Merge branch 'master' into reset_index-duplicate-labels
johnzangwill Dec 10, 2021
13c6dce
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 11, 2021
2b37ab3
Trigger CI
johnzangwill Dec 11, 2021
140b5d9
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 11, 2021
1bd1cbd
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 11, 2021
e27df82
Change from None to use_flag
johnzangwill Dec 12, 2021
749838c
Get rid of use_flag altogether
johnzangwill Dec 12, 2021
c7cf483
Update frame.py
johnzangwill Dec 12, 2021
7711474
Added version and improved tests
johnzangwill Dec 14, 2021
0ecf6fd
Trigger CI
johnzangwill Dec 14, 2021
0730d7b
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 14, 2021
99ea2c3
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 14, 2021
250999a
Trigger CI
johnzangwill Dec 14, 2021
090468e
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 14, 2021
14a07e1
Trigger CI
johnzangwill Dec 15, 2021
8eb8710
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 15, 2021
18341db
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 20, 2021
1337283
Trigger CI
johnzangwill Dec 20, 2021
a8dab78
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 20, 2021
95cc65b
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 20, 2021
7de074c
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 22, 2021
bb54268
allow_duplicates lib.no_default
johnzangwill Dec 24, 2021
01136ae
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 24, 2021
d21846b
Trigger CI
johnzangwill Dec 24, 2021
3ef667c
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 24, 2021
22deac4
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 26, 2021
540b307
Correct overloads
johnzangwill Dec 27, 2021
8cc81b0
Merge branch 'reset_index-duplicate-labels' of https://github.com/joh…
johnzangwill Dec 27, 2021
fc74265
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 27, 2021
a59644a
Update frame.py
johnzangwill Dec 27, 2021
b774598
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 27, 2021
67a5956
Update frame.py
johnzangwill Dec 27, 2021
e88b8e2
Merge branch 'reset_index-duplicate-labels' of https://github.com/joh…
johnzangwill Dec 27, 2021
9ad3026
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 27, 2021
1fdc39f
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Dec 29, 2021
7639464
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Jan 1, 2022
c58d992
Merge branch 'pandas-dev:master' into reset_index-duplicate-labels
johnzangwill Jan 2, 2022
e07eea6
Merge branch 'main' into reset_index-duplicate-labels
johnzangwill Jan 17, 2022
04b3a38
Removed docstring Raises
johnzangwill Jan 17, 2022
2f94170
Version to 1.5
johnzangwill Jan 17, 2022
9241f96
Merge branch 'pandas-dev:main' into reset_index-duplicate-labels
johnzangwill Jan 18, 2022
0b84426
Merge branch 'pandas-dev:main' into reset_index-duplicate-labels
johnzangwill Jan 19, 2022
c2bed8f
Merge branch 'pandas-dev:main' into reset_index-duplicate-labels
johnzangwill Jan 20, 2022
3530ca8
Merge branch 'main' into reset_index-duplicate-labels
johnzangwill Jan 23, 2022
dddae07
Merge branch 'pandas-dev:main' into reset_index-duplicate-labels
johnzangwill Jan 24, 2022
14edbdb
Update series.py
johnzangwill Jan 24, 2022
8383572
Merge branch 'reset_index-duplicate-labels' of https://github.com/joh…
johnzangwill Jan 24, 2022
37cc560
Merge branch 'pandas-dev:main' into reset_index-duplicate-labels
johnzangwill Jan 24, 2022
3625d77
Merge branch 'pandas-dev:main' into reset_index-duplicate-labels
johnzangwill Jan 24, 2022
9d0f798
Merge branch 'pandas-dev:main' into reset_index-duplicate-labels
johnzangwill Jan 25, 2022
e2fbf3e
Merge branch 'pandas-dev:main' into reset_index-duplicate-labels
johnzangwill Jan 25, 2022
0b2a9d1
Merge branch 'pandas-dev:main' into reset_index-duplicate-labels
johnzangwill Jan 26, 2022
c5618c8
Merge branch 'pandas-dev:main' into reset_index-duplicate-labels
johnzangwill Jan 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5838,7 +5838,12 @@ class max type
level_values, lab, allow_fill=True, fill_value=lev._na_value
)

new_obj.insert(0, name, level_values)
new_obj.insert(
0,
name,
level_values,
allow_duplicates=self.flags.allows_duplicate_labels,
)

new_obj.index = new_index
if not inplace:
Expand Down
15 changes: 1 addition & 14 deletions pandas/tests/frame/methods/test_reset_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,21 +328,13 @@ 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

with tm.assert_produces_warning(warn):
result = df.reset_index()

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":
Expand Down Expand Up @@ -374,11 +366,6 @@ 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#16164: multiindex (tuple) full key
result = df.set_index([("A", "")]).reset_index()
tm.assert_frame_equal(result, df)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/pytables/test_put.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def make_index(names=None):
columns=["a", "b"],
index=make_index(["date", "a", "t"]),
)
msg = "duplicate names/columns in the multi-index when storing as a table"
msg = "cannot reindex on an axis with duplicate labels"
with pytest.raises(ValueError, match=msg):
store.append("df", df)

Expand Down