-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: DataFrame.drop raising TypeError for string label with non-unique MultiIndex and no level provided #38220
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
Conversation
pandas/core/generic.py
Outdated
@@ -4173,6 +4173,14 @@ def _drop_axis( | |||
|
|||
# Case for non-unique axis | |||
else: | |||
if ( |
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.
should this case only be an issue starting on L4193? e.g. level is None there
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.
Yep, this is correct. Question is what to do when level is None.
I think ~axis.get_level_values(level).isin(labels)
is the way to go, so I check before reaching the if else block if level is None to set it to 0 in this case
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.
i think setting to level = 0 is fine, i would just move the check inside the block on L4193 as its not going to be true where it is if level is not None in the first place.
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.
This would work as an alternative
pandas/core/generic.py
Outdated
@@ -4173,6 +4173,14 @@ def _drop_axis( | |||
|
|||
# Case for non-unique axis | |||
else: | |||
if ( |
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.
i think setting to level = 0 is fine, i would just move the check inside the block on L4193 as its not going to be true where it is if level is not None in the first place.
pandas/core/generic.py
Outdated
labels_missing = (axis.get_indexer_for(labels) == -1).any() | ||
if errors == "raise" and labels_missing: | ||
raise KeyError(f"{labels} not found in axis") | ||
if isinstance(axis, MultiIndex) and labels.dtype == "object": |
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.
make an elif here
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
thanks @phofl |
…e MultiIndex and no level provided (pandas-dev#38220)
DataFrame.drop
fails when there is a multiIndex without any level provided #36293black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff
As far as I know, isin needs a level input when only a string is given for a MultiIndex. If this should not change, we have to set level=0 in this case.