-
-
Notifications
You must be signed in to change notification settings - Fork 18.9k
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?
Changes from 9 commits
3df788a
fe07d18
f57ddc5
eca19a2
390f3ef
629bdf9
041994c
d881247
3e63865
7f541de
703085e
3793317
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2084,7 +2084,24 @@ def _validate_index_level(self, level) -> None: | |||||||||||
verification must be done like in MultiIndex. | ||||||||||||
|
||||||||||||
""" | ||||||||||||
if isinstance(level, int): | ||||||||||||
# Explicitly raise for missing/null values to match pandas convention | ||||||||||||
# Also reject all NA-like values (np.nan, pd.NA, pd.NaT, etc.) | ||||||||||||
if isna(level): | ||||||||||||
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. what if index.name is a na-like scalar? |
||||||||||||
raise KeyError( | ||||||||||||
f"Requested level ({level}) does not match index name ({self.name})" | ||||||||||||
) | ||||||||||||
|
||||||||||||
# Reject booleans unless the index name is actually a boolean and matches | ||||||||||||
if isinstance(level, bool): | ||||||||||||
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. isnt this handled in the existing 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. Makes sense... I'll keep the same. |
||||||||||||
if level != self.name: | ||||||||||||
raise KeyError( | ||||||||||||
f"Requested level ({level}) does not match index name ({self.name})" | ||||||||||||
) | ||||||||||||
return | ||||||||||||
|
||||||||||||
# Integer-like levels | ||||||||||||
if lib.is_integer(level): | ||||||||||||
# Exclude bools (already handled above) | ||||||||||||
if level < 0 and level != -1: | ||||||||||||
raise IndexError( | ||||||||||||
"Too many levels: Index has only 1 level, " | ||||||||||||
|
@@ -2094,7 +2111,11 @@ def _validate_index_level(self, level) -> None: | |||||||||||
raise IndexError( | ||||||||||||
f"Too many levels: Index has only 1 level, not {level + 1}" | ||||||||||||
) | ||||||||||||
elif level != self.name: | ||||||||||||
return | ||||||||||||
|
||||||||||||
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. I think that this is a better verification that validates what is expected from the documentation.
Suggested change
|
||||||||||||
# For all other types, require exact match to index name | ||||||||||||
# Use pandas' isna for both level and self.name to catch NA-like names | ||||||||||||
if isna(self.name) or level != self.name: | ||||||||||||
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.
seems this change is unrelated to the rest of the PR?
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.
Yes, actually, I was trying something and forgot to undo this change. It's fixed in the latest commit.