diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 9210f1e0082f0..dea4c6801227c 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -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`) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index d75479da70d11..8d3e9d4b94af3 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -38,6 +38,7 @@ is_datetime_array, no_default, ) +from pandas._libs.missing import is_matching_na from pandas._libs.tslibs import ( OutOfBoundsDatetime, Timestamp, @@ -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, " @@ -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) diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index 21e6e2efbe778..9851975f3ae02 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -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 3 False dtype: bool """

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