Skip to content

BUG: more specific .drop() message when keys not found #40177

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

Closed
wants to merge 1 commit into from
Closed

BUG: more specific .drop() message when keys not found #40177

wants to merge 1 commit into from

Conversation

nishithshowri006
Copy link

Changed "not found in axis" to "not found in columns"

Changed "not found in axis" to "not found in columns"
@@ -5866,7 +5866,7 @@ def drop(self: _IndexT, labels, errors: str_t = "raise") -> _IndexT:
mask = indexer == -1
if mask.any():
if errors != "ignore":
raise KeyError(f"{labels[mask]} not found in axis")
raise KeyError(f"{labels[mask]} not found in columns")
Copy link
Member

Choose a reason for hiding this comment

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

You can not do it this way. We are running through here too, if the user drops something from the index. This would be very misleading. Also you have to adjust tests

Copy link
Author

Choose a reason for hiding this comment

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

Sure, so what do you think will be the right way to do ?

Copy link
Contributor

Choose a reason for hiding this comment

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

@phofl Does Index have an attribute that determines whether it is a column or index axis? If it does, it could use that the change the error message. If not, an alternative is to catch this error in DataFrame.drop and rethrow with a more specific message.

@jreback jreback changed the title BUG: REF to #40160 BUG: more specific .drop() message when keys not found Mar 2, 2021
@jreback jreback added Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves labels Mar 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG:df.drop(columns=["col1"]) KeyError "['col1'] not found in axis"
4 participants