diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index fcf576efb73ab..69aeb41a1c04f 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5058,7 +5058,7 @@ def get_value(self, series: Series, key): # try that loc = self.get_loc(key) except KeyError: - if not self._should_fallback_to_positional(): + if not self._should_fallback_to_positional: raise elif is_integer(key): # If the Index cannot hold integer, then this is unambiguously @@ -5075,6 +5075,7 @@ def _check_indexing_error(self, key): # would convert to numpy arrays and raise later any way) - GH29926 raise InvalidIndexError(key) + @cache_readonly def _should_fallback_to_positional(self) -> bool: """ Should an integer key be treated as positional? diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 7c4a2e31b96d1..f77236091b09b 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -743,6 +743,7 @@ def _convert_slice_indexer(self, key: slice, kind: str): return super()._convert_slice_indexer(key, kind) + @cache_readonly def _should_fallback_to_positional(self) -> bool: # integer lookups in Series.__getitem__ are unambiguously # positional in this case diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 0876847aed84f..2d5e37e79389e 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -2554,12 +2554,13 @@ def _check_indexing_error(self, key): # We have to explicitly exclude generators, as these are hashable. raise InvalidIndexError(key) + @cache_readonly def _should_fallback_to_positional(self) -> bool: """ Should integer key(s) be treated as positional? """ # GH#33355 - return self.levels[0]._should_fallback_to_positional() + return self.levels[0]._should_fallback_to_positional def _get_values_for_loc(self, series: Series, loc, key): """ diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index ea2d5d9eec6ac..fd4259c7037e9 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -228,6 +228,7 @@ def astype(self, dtype, copy=True): # ---------------------------------------------------------------- # Indexing Methods + @cache_readonly @doc(Index._should_fallback_to_positional) def _should_fallback_to_positional(self) -> bool: return False diff --git a/pandas/core/series.py b/pandas/core/series.py index 59ea6710ea6cd..99ae177aaae6c 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -938,7 +938,7 @@ def __getitem__(self, key): if isinstance(key, (list, tuple)): key = unpack_1tuple(key) - if is_integer(key) and self.index._should_fallback_to_positional(): + if is_integer(key) and self.index._should_fallback_to_positional: return self._values[key] elif key_is_scalar: @@ -1000,7 +1000,7 @@ def _get_with(self, key): if key_type == "integer": # We need to decide whether to treat this as a positional indexer # (i.e. self.iloc) or label-based (i.e. self.loc) - if not self.index._should_fallback_to_positional(): + if not self.index._should_fallback_to_positional: return self.loc[key] else: return self.iloc[key] @@ -1122,7 +1122,7 @@ def _set_with(self, key, value): # Note: key_type == "boolean" should not occur because that # should be caught by the is_bool_indexer check in __setitem__ if key_type == "integer": - if not self.index._should_fallback_to_positional(): + if not self.index._should_fallback_to_positional: self._set_labels(key, value) else: self._set_values(key, value) diff --git a/pandas/tests/indexing/multiindex/test_partial.py b/pandas/tests/indexing/multiindex/test_partial.py index a99f09143e282..50a31f2fd22c6 100644 --- a/pandas/tests/indexing/multiindex/test_partial.py +++ b/pandas/tests/indexing/multiindex/test_partial.py @@ -158,8 +158,8 @@ def test_getitem_intkey_leading_level( assert isinstance(mi.levels[0], Float64Index) assert 14 not in mi.levels[0] - assert not mi.levels[0]._should_fallback_to_positional() - assert not mi._should_fallback_to_positional() + assert not mi.levels[0]._should_fallback_to_positional + assert not mi._should_fallback_to_positional with pytest.raises(KeyError, match="14"): ser[14]