Skip to content

Commit 041994c

Browse files
committed
BUG: Enhance Index level validation to explicitly handle NA values and improve error messaging
1 parent 629bdf9 commit 041994c

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

pandas/core/indexes/base.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,11 +2084,21 @@ def _validate_index_level(self, level) -> None:
20842084
verification must be done like in MultiIndex.
20852085
20862086
"""
2087-
if lib.is_integer(level):
2088-
if isinstance(self.name, int) and level == self.name:
2089-
return
2087+
# Explicitly raise for missing/null values to match pandas convention
2088+
if isna(level):
2089+
raise KeyError(
2090+
"Requested level is NA/NaN/NaT, which is not a valid level name"
2091+
)
20902092

2091-
if level < 0 and level != -1:
2093+
# Standard integer check, but reject bool
2094+
if lib.is_integer(level) and not isinstance(level, bool):
2095+
# If the index itself is named as integer, accept
2096+
if lib.is_integer(self.name) and level == self.name:
2097+
return
2098+
# Only allow 0 and -1 for a single-level Index
2099+
if level in (0, -1):
2100+
return
2101+
if level < 0:
20922102
raise IndexError(
20932103
"Too many levels: Index has only 1 level, "
20942104
f"{level} is not a valid level number"
@@ -2098,13 +2108,20 @@ def _validate_index_level(self, level) -> None:
20982108
f"Too many levels: Index has only 1 level, not {level + 1}"
20992109
)
21002110

2101-
elif (
2102-
isinstance(level, str) and isinstance(self.name, str) and level != self.name
2111+
# String level: only match if name is exactly the same string
2112+
elif isinstance(level, str) and not (
2113+
isinstance(self.name, str) and level == self.name
21032114
):
21042115
raise KeyError(
21052116
f"Requested level ({level}) does not match index name ({self.name})"
21062117
)
21072118

2119+
# If level type is not int, str, or is NA, always raise KeyError
2120+
else:
2121+
raise KeyError(
2122+
f"Requested level ({level}) is not a valid level name or number"
2123+
)
2124+
21082125
def _get_level_number(self, level) -> int:
21092126
self._validate_index_level(level)
21102127
return 0

0 commit comments

Comments
 (0)