Skip to content

Commit 1832373

Browse files
jbrockmendelJulianWgs
authored andcommitted
PERF: cache _should_fallback_to_positional (pandas-dev#41917)
1 parent f670a67 commit 1832373

File tree

6 files changed

+11
-7
lines changed

6 files changed

+11
-7
lines changed

pandas/core/indexes/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5101,7 +5101,7 @@ def get_value(self, series: Series, key):
51015101
# try that
51025102
loc = self.get_loc(key)
51035103
except KeyError:
5104-
if not self._should_fallback_to_positional():
5104+
if not self._should_fallback_to_positional:
51055105
raise
51065106
elif is_integer(key):
51075107
# If the Index cannot hold integer, then this is unambiguously
@@ -5118,6 +5118,7 @@ def _check_indexing_error(self, key):
51185118
# would convert to numpy arrays and raise later any way) - GH29926
51195119
raise InvalidIndexError(key)
51205120

5121+
@cache_readonly
51215122
def _should_fallback_to_positional(self) -> bool:
51225123
"""
51235124
Should an integer key be treated as positional?

pandas/core/indexes/interval.py

+1
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ def _convert_slice_indexer(self, key: slice, kind: str):
740740

741741
return super()._convert_slice_indexer(key, kind)
742742

743+
@cache_readonly
743744
def _should_fallback_to_positional(self) -> bool:
744745
# integer lookups in Series.__getitem__ are unambiguously
745746
# positional in this case

pandas/core/indexes/multi.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2552,12 +2552,13 @@ def _check_indexing_error(self, key):
25522552
# We have to explicitly exclude generators, as these are hashable.
25532553
raise InvalidIndexError(key)
25542554

2555+
@cache_readonly
25552556
def _should_fallback_to_positional(self) -> bool:
25562557
"""
25572558
Should integer key(s) be treated as positional?
25582559
"""
25592560
# GH#33355
2560-
return self.levels[0]._should_fallback_to_positional()
2561+
return self.levels[0]._should_fallback_to_positional
25612562

25622563
def _get_values_for_loc(self, series: Series, loc, key):
25632564
"""

pandas/core/indexes/numeric.py

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ def astype(self, dtype, copy=True):
228228
# ----------------------------------------------------------------
229229
# Indexing Methods
230230

231+
@cache_readonly
231232
@doc(Index._should_fallback_to_positional)
232233
def _should_fallback_to_positional(self) -> bool:
233234
return False

pandas/core/series.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ def __getitem__(self, key):
938938
if isinstance(key, (list, tuple)):
939939
key = unpack_1tuple(key)
940940

941-
if is_integer(key) and self.index._should_fallback_to_positional():
941+
if is_integer(key) and self.index._should_fallback_to_positional:
942942
return self._values[key]
943943

944944
elif key_is_scalar:
@@ -1000,7 +1000,7 @@ def _get_with(self, key):
10001000
if key_type == "integer":
10011001
# We need to decide whether to treat this as a positional indexer
10021002
# (i.e. self.iloc) or label-based (i.e. self.loc)
1003-
if not self.index._should_fallback_to_positional():
1003+
if not self.index._should_fallback_to_positional:
10041004
return self.loc[key]
10051005
else:
10061006
return self.iloc[key]
@@ -1124,7 +1124,7 @@ def _set_with(self, key, value):
11241124
# Note: key_type == "boolean" should not occur because that
11251125
# should be caught by the is_bool_indexer check in __setitem__
11261126
if key_type == "integer":
1127-
if not self.index._should_fallback_to_positional():
1127+
if not self.index._should_fallback_to_positional:
11281128
self._set_labels(key, value)
11291129
else:
11301130
self._set_values(key, value)

pandas/tests/indexing/multiindex/test_partial.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ def test_getitem_intkey_leading_level(
158158
assert isinstance(mi.levels[0], Float64Index)
159159

160160
assert 14 not in mi.levels[0]
161-
assert not mi.levels[0]._should_fallback_to_positional()
162-
assert not mi._should_fallback_to_positional()
161+
assert not mi.levels[0]._should_fallback_to_positional
162+
assert not mi._should_fallback_to_positional
163163

164164
with pytest.raises(KeyError, match="14"):
165165
ser[14]

0 commit comments

Comments
 (0)