Skip to content

PERF: cache _should_fallback_to_positional #41917

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

Merged
merged 1 commit into from
Jun 17, 2021
Merged
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
3 changes: 2 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
1 change: 1 addition & 0 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexing/multiindex/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down