-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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 all commits
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
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4392,7 +4392,7 @@ def insert( | |
loc: int, | ||
column: Hashable, | ||
value: Scalar | AnyArrayLike, | ||
allow_duplicates: bool = False, | ||
allow_duplicates: bool | lib.NoDefault = lib.no_default, | ||
) -> None: | ||
""" | ||
Insert column into DataFrame at specified location. | ||
|
@@ -4407,7 +4407,7 @@ def insert( | |
column : str, number, or hashable object | ||
Label of the inserted column. | ||
value : Scalar, Series, or array-like | ||
allow_duplicates : bool, optional default False | ||
allow_duplicates : bool, optional, default lib.no_default | ||
|
||
See Also | ||
-------- | ||
|
@@ -4439,6 +4439,8 @@ def insert( | |
0 NaN 100 1 99 3 | ||
1 5.0 100 2 99 4 | ||
""" | ||
if allow_duplicates is lib.no_default: | ||
allow_duplicates = False | ||
if allow_duplicates and not self.flags.allows_duplicate_labels: | ||
raise ValueError( | ||
"Cannot specify 'allow_duplicates=True' when " | ||
|
@@ -5581,6 +5583,7 @@ def reset_index( | |
inplace: Literal[False] = ..., | ||
col_level: Hashable = ..., | ||
col_fill: Hashable = ..., | ||
allow_duplicates: bool | lib.NoDefault = ..., | ||
) -> DataFrame: | ||
... | ||
|
||
|
@@ -5592,6 +5595,7 @@ def reset_index( | |
inplace: Literal[True], | ||
col_level: Hashable = ..., | ||
col_fill: Hashable = ..., | ||
allow_duplicates: bool | lib.NoDefault = ..., | ||
) -> None: | ||
... | ||
|
||
|
@@ -5603,6 +5607,7 @@ def reset_index( | |
inplace: Literal[True], | ||
col_level: Hashable = ..., | ||
col_fill: Hashable = ..., | ||
allow_duplicates: bool | lib.NoDefault = ..., | ||
) -> None: | ||
... | ||
|
||
|
@@ -5614,6 +5619,7 @@ def reset_index( | |
inplace: Literal[True], | ||
col_level: Hashable = ..., | ||
col_fill: Hashable = ..., | ||
allow_duplicates: bool | lib.NoDefault = ..., | ||
) -> None: | ||
... | ||
|
||
|
@@ -5624,6 +5630,7 @@ def reset_index( | |
inplace: Literal[True], | ||
col_level: Hashable = ..., | ||
col_fill: Hashable = ..., | ||
allow_duplicates: bool | lib.NoDefault = ..., | ||
) -> None: | ||
... | ||
|
||
|
@@ -5635,6 +5642,7 @@ def reset_index( | |
inplace: bool = ..., | ||
col_level: Hashable = ..., | ||
col_fill: Hashable = ..., | ||
allow_duplicates: bool | lib.NoDefault = ..., | ||
) -> DataFrame | None: | ||
... | ||
|
||
|
@@ -5646,6 +5654,7 @@ def reset_index( | |
inplace: bool = False, | ||
col_level: Hashable = 0, | ||
col_fill: Hashable = "", | ||
allow_duplicates: bool | lib.NoDefault = lib.no_default, | ||
) -> DataFrame | None: | ||
""" | ||
Reset the index, or a level of it. | ||
|
@@ -5671,6 +5680,10 @@ def reset_index( | |
col_fill : object, default '' | ||
If the columns have multiple levels, determines how the other | ||
levels are named. If None then the index name is repeated. | ||
allow_duplicates : bool, optional, default lib.no_default | ||
Allow duplicate column labels to be created. | ||
|
||
.. versionadded:: 1.5.0 | ||
|
||
Returns | ||
------- | ||
|
@@ -5794,6 +5807,8 @@ class max type | |
new_obj = self | ||
else: | ||
new_obj = self.copy() | ||
if allow_duplicates is not lib.no_default: | ||
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. shouldnt we do the same as you are doing in insert and just set this here e.g.
simpler & more readable 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. @jreback Your suggestion has got to be a typo. That change would render the argument always If I take out the next line, which is type-checking, then I will have to take out my test https://github.com/johnzangwill/pandas/blob/dddae0734e37ab9fa4ae4a89e68043c4631fd68c/pandas/tests/frame/methods/test_reset_index.py#L474 If I copy in the code from I only used I would be more than happy to take it out and go back to the original much simpler @jreback Let me know what you want:
cc @rhshadrach, @phofl, @mroeschke. Comments on this? |
||
allow_duplicates = validate_bool_kwarg(allow_duplicates, "allow_duplicates") | ||
|
||
new_index = default_index(len(new_obj)) | ||
if level is not None: | ||
|
@@ -5845,7 +5860,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=allow_duplicates, | ||
) | ||
|
||
new_obj.index = new_index | ||
if not inplace: | ||
|
Uh oh!
There was an error while loading. Please reload this page.