Skip to content

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

Merged
merged 9 commits into from
Dec 14, 2020

Conversation

phofl
Copy link
Member

@phofl phofl commented Dec 1, 2020

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.

@@ -4173,6 +4173,14 @@ def _drop_axis(

# Case for non-unique axis
else:
if (
Copy link
Contributor

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

Copy link
Member Author

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

Copy link
Contributor

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.

Copy link
Member Author

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

@@ -4173,6 +4173,14 @@ def _drop_axis(

# Case for non-unique axis
else:
if (
Copy link
Contributor

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.

@pep8speaks
Copy link

pep8speaks commented Dec 13, 2020

Hello @phofl! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-12-13 17:21:57 UTC

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":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make an elif here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@jreback jreback added this to the 1.3 milestone Dec 13, 2020
@jreback jreback added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Dec 13, 2020
@jreback jreback merged commit ebe760e into pandas-dev:master Dec 14, 2020
@jreback
Copy link
Contributor

jreback commented Dec 14, 2020

thanks @phofl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: DataFrame.drop fails when there is a multiIndex without any level provided
3 participants