-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
BUG: Fix Index.get_level_values() mishandling of boolean, pd.NA, np.nan, and pd.NaT levels #62175
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
base: main
Are you sure you want to change the base?
Conversation
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 solves the boolean case, but it will have the same problems for the NA
, nan
and NaT
and perhaps other edge cases.
I think that its best for one of the pandas core members decide on how non-string level and Index.name should be handled.
pandas/core/indexes/base.py
Outdated
@@ -2084,7 +2084,7 @@ def _validate_index_level(self, level) -> None: | |||
verification must be done like in MultiIndex. | |||
|
|||
""" | |||
if isinstance(level, int): | |||
if type(level) is int: |
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.
Just a simple edge case where the Index.name is also an int
if type(level) is int: | |
if type(level) is int: | |
if isinstance(self.name, int) and level == self.name: | |
return |
raise KeyError( | ||
f"Requested level ({level}) does not match index name ({self.name})" | ||
) | ||
|
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 that this is a better verification that validates what is expected from the documentation.
elif isinstance(level, str) and isinstance(self.name, str) and level != self.name: | |
raise KeyError( | |
f"Requested level ({level}) does not match index name ({self.name})" | |
) |
pandas/core/indexes/base.py
Outdated
@@ -2084,7 +2084,10 @@ def _validate_index_level(self, level) -> None: | |||
verification must be done like in MultiIndex. | |||
""" | |||
if isinstance(level, int): | |||
if type(level) is int: |
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.
lib.is_integer
…d improve error messaging
level
correctly #62169