Skip to content

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

whyvineet
Copy link
Contributor

Copy link

@Alvaro-Kothe Alvaro-Kothe left a 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.

@@ -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:

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

Suggested change
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})"
)

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.

Suggested change
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})"
)

@@ -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:
Copy link
Member

Choose a reason for hiding this comment

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

lib.is_integer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Index.get_level_values() does not handle boolean, pd.NA, np.nan, or pd.NaT level correctly
3 participants