Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

BUG: Fix Index.get_level_values() with boolean, NaN, NA, NaT names #62392

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
vgnshiyer wants to merge 6 commits into pandas-dev:main
base: main
Choose a base branch
Loading
from vgnshiyer:fix-get-level-values-special-names
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,7 @@ Other
- Bug in :meth:`DataFrame.transform` that was returning the wrong order unless the index was monotonically increasing. (:issue:`57069`)
- Bug in :meth:`DataFrame.where` where using a non-bool type array in the function would return a ``ValueError`` instead of a ``TypeError`` (:issue:`56330`)
- Bug in :meth:`Index.sort_values` when passing a key function that turns values into tuples, e.g. ``key=natsort.natsort_key``, would raise ``TypeError`` (:issue:`56081`)
- Bug in :meth:`Index.get_level_values` incorrectly handling boolean, ``np.nan``, ``pd.NA``, and ``pd.NaT`` level names (:issue:`TBD`)
- Bug in :meth:`MultiIndex.fillna` error message was referring to ``isna`` instead of ``fillna`` (:issue:`60974`)
- Bug in :meth:`Series.describe` where median percentile was always included when the ``percentiles`` argument was passed (:issue:`60550`).
- Bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
Expand Down
21 changes: 17 additions & 4 deletions pandas/core/indexes/base.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
is_datetime_array,
no_default,
)
from pandas._libs.missing import is_matching_na
from pandas._libs.tslibs import (
OutOfBoundsDatetime,
Timestamp,
Expand Down Expand Up @@ -2087,7 +2088,9 @@ def _validate_index_level(self, level) -> None:
verification must be done like in MultiIndex.

"""
if isinstance(level, int):
if lib.is_integer(level):
if lib.is_integer(self.name) and self.name == level:
return
if level < 0 and level != -1:
raise IndexError(
"Too many levels: Index has only 1 level, "
Expand All @@ -2097,10 +2100,20 @@ def _validate_index_level(self, level) -> None:
raise IndexError(
f"Too many levels: Index has only 1 level, not {level + 1}"
)
return
mismatch_error_msg = (
f"Requested level ({level}) does not match index name ({self.name})"
)
if lib.is_integer(self.name):
raise KeyError(mismatch_error_msg)
if isna(level) and isna(self.name):
if not is_matching_na(level, self.name):
raise KeyError(mismatch_error_msg)
return
elif isna(level) or isna(self.name):
raise KeyError(mismatch_error_msg)
elif level != self.name:
raise KeyError(
f"Requested level ({level}) does not match index name ({self.name})"
)
raise KeyError(mismatch_error_msg)

def _get_level_number(self, level) -> int:
self._validate_index_level(level)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/strings/accessor.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3610,8 +3610,8 @@ def casefold(self):
>>> s3 = pd.Series(['23', '3', '1⁄5', ''])
>>> s3.str.isdigit()
0 True
1 False
2 False
1 True
2 True
Comment on lines -3613 to +3614
Copy link
Contributor

@Alvaro-Kothe Alvaro-Kothe Sep 24, 2025

Choose a reason for hiding this comment

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

These changes are unrelated, please remove them.

3 False
dtype: bool
"""
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /