Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pandas/_libs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__all__ = [
"NA",
"Interval",
"NaT",
"NaTType",
Expand All @@ -16,6 +17,7 @@
import pandas._libs.pandas_parser # isort: skip # type: ignore[reportUnusedImport]
import pandas._libs.pandas_datetime # noqa: F401 # isort: skip # type: ignore[reportUnusedImport]
from pandas._libs.interval import Interval
from pandas._libs.missing import NA
Copy link
Member

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?

Copy link
Contributor Author

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.

from pandas._libs.tslibs import (
NaT,
NaTType,
Expand Down
25 changes: 23 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Member

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

isnt this handled in the existing elif level != self.name check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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, "
Expand All @@ -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

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

# 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})"
)
Expand Down
Loading