Skip to content
Open
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
3df788a
BUG: Fix Index.get_level_values() mishandling of boolean, pd.NA, np.n…
whyvineet Aug 23, 2025
fe07d18
CLN: Remove redundant checks for NA, NaT, and NaN in Index class
whyvineet Aug 23, 2025
f57ddc5
CLN: Move import of NA to maintain consistent import order
whyvineet Aug 23, 2025
eca19a2
BUG: Fix Index level validation to handle integer index names correctly
whyvineet Aug 23, 2025
390f3ef
CLN: Remove unnecessary blank line and simplify condition in Index cl…
whyvineet Aug 23, 2025
629bdf9
BUG: Update Index class level validation to use lib.is_integer for ty…
whyvineet Aug 23, 2025
041994c
BUG: Enhance Index level validation to explicitly handle NA values an…
whyvineet Aug 24, 2025
d881247
BUG: Improve Index level validation to reject all NA-like values and …
whyvineet Aug 25, 2025
3e63865
BUG: Update error message for NA-like level validation to include lev…
whyvineet Aug 25, 2025
7f541de
BUG: Enhance index level validation to handle NA-like index names and…
whyvineet Aug 25, 2025
703085e
BUG: Refactor error message formatting in Index class for clarity
whyvineet Aug 25, 2025
3793317
BUG: Refactor index level validation to improve handling of NA-like v…
whyvineet Aug 25, 2025
d32d836
Merge branch 'pandas-dev:main' into main
whyvineet Oct 4, 2025
c03d480
Fix _validate_index_level to handle None values correctly and fix CRL…
whyvineet Oct 4, 2025
19832f3
Merge branch 'pandas-dev:main' into main
whyvineet Nov 9, 2025
198a309
ENH: Add validation for positional levels and improve error messages …
whyvineet Dec 7, 2025
dd0960a
Merge branch 'pandas-dev:main' into main
whyvineet Dec 7, 2025
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
44 changes: 40 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2127,16 +2127,52 @@ def _validate_index_level(self, level) -> None:
verification must be done like in MultiIndex.

"""
if isinstance(level, int):
if isna(level) and isna(self.name):
Copy link
Member

Choose a reason for hiding this comment

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

is_matching_na

return

elif isna(level) and not isna(self.name):
raise KeyError(
f"Requested level ({level}) does not match index name ({self.name})"
)

elif not isna(level) and isna(self.name):
# level is not NA, but self.name is NA
# This is valid for integer levels (0, -1) accessing unnamed index
if lib.is_integer(level):
if level < 0 and level != -1:
raise IndexError(
f"Too many levels: Index has only 1 level, not {level + 1}"
)
elif level > 0:
raise IndexError(
f"Too many levels: Index has only 1 level, not {level + 1}"
)
return
else:
raise KeyError(
f"Requested level ({level}) does not match index name ({self.name})"
)

elif lib.is_integer(level):
if isinstance(self.name, int) and level == self.name:
return
if level < 0 and level != -1:
raise IndexError(
"Too many levels: Index has only 1 level, "
f"{level} is not a valid level number"
f"Too many levels: Index has only 1 level, not {level + 1}"
)
if level > 0:
elif level > 0:
raise IndexError(
f"Too many levels: Index has only 1 level, not {level + 1}"
)
return

Copy link
Member

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

elif isinstance(level, str) and isinstance(self.name, str):
if level != self.name:
raise KeyError(
f"Requested level ({level}) does not match index name ({self.name})"
)
return

elif level != self.name:
raise KeyError(
f"Requested level ({level}) does not match index name ({self.name})"
Expand Down
Loading