Skip to content

Commit e5c65bf

Browse files
authored
REF: Index.get_value call self.get_loc instead of self._engine.get_loc (#31713)
1 parent aa720d4 commit e5c65bf

File tree

4 files changed

+26
-49
lines changed

4 files changed

+26
-49
lines changed

pandas/core/indexes/base.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -4590,9 +4590,9 @@ def get_value(self, series: "Series", key):
45904590
# If that fails, raise a KeyError if an integer
45914591
# index, otherwise, see if key is an integer, and
45924592
# try that
4593-
loc = self._engine.get_loc(key)
4593+
loc = self.get_loc(key)
45944594
except KeyError:
4595-
if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
4595+
if not self._should_fallback_to_positional():
45964596
raise
45974597
elif is_integer(key):
45984598
# If the Index cannot hold integer, then this is unambiguously
@@ -4603,6 +4603,14 @@ def get_value(self, series: "Series", key):
46034603

46044604
return self._get_values_for_loc(series, loc)
46054605

4606+
def _should_fallback_to_positional(self) -> bool:
4607+
"""
4608+
If an integer key is not found, should we fall back to positional indexing?
4609+
"""
4610+
if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
4611+
return False
4612+
return True
4613+
46064614
def _get_values_for_loc(self, series: "Series", loc):
46074615
"""
46084616
Do a positional lookup on the given Series, returning either a scalar

pandas/core/indexes/extension.py

+1-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Shared methods for Index subclasses backed by ExtensionArray.
33
"""
4-
from typing import TYPE_CHECKING, List
4+
from typing import List
55

66
import numpy as np
77

@@ -11,7 +11,6 @@
1111
from pandas.core.dtypes.common import (
1212
ensure_platform_int,
1313
is_dtype_equal,
14-
is_integer,
1514
is_object_dtype,
1615
)
1716
from pandas.core.dtypes.generic import ABCSeries
@@ -21,9 +20,6 @@
2120
from pandas.core.indexes.base import Index
2221
from pandas.core.ops import get_op_result_name
2322

24-
if TYPE_CHECKING:
25-
from pandas import Series
26-
2723

2824
def inherit_from_data(name: str, delegate, cache: bool = False, wrap: bool = False):
2925
"""
@@ -297,26 +293,3 @@ def astype(self, dtype, copy=True):
297293
# pass copy=False because any copying will be done in the
298294
# _data.astype call above
299295
return Index(new_values, dtype=new_values.dtype, name=self.name, copy=False)
300-
301-
# --------------------------------------------------------------------
302-
# Indexing Methods
303-
304-
@Appender(Index.get_value.__doc__)
305-
def get_value(self, series: "Series", key):
306-
"""
307-
Fast lookup of value from 1-dimensional ndarray. Only use this if you
308-
know what you're doing
309-
"""
310-
try:
311-
loc = self.get_loc(key)
312-
except KeyError:
313-
# e.g. DatetimeIndex doesn't hold integers
314-
if is_integer(key) and not self.holds_integer():
315-
# Fall back to positional
316-
loc = key
317-
else:
318-
raise
319-
320-
return self._get_values_for_loc(series, loc)
321-
322-
# --------------------------------------------------------------------

pandas/core/indexes/interval.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,10 @@ def is_overlapping(self) -> bool:
523523
# GH 23309
524524
return self._engine.is_overlapping
525525

526-
def holds_integer(self):
527-
return self.dtype.subtype.kind not in ["m", "M"]
528-
# TODO: There must already exist something for this?
526+
def _should_fallback_to_positional(self):
527+
# integer lookups in Series.__getitem__ are unambiguously
528+
# positional in this case
529+
return self.dtype.subtype.kind in ["m", "M"]
529530

530531
@Appender(Index._convert_scalar_indexer.__doc__)
531532
def _convert_scalar_indexer(self, key, kind=None):

pandas/core/indexes/numeric.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TYPE_CHECKING, Any
1+
from typing import Any
22

33
import numpy as np
44

@@ -32,12 +32,9 @@
3232

3333
from pandas.core import algorithms
3434
import pandas.core.common as com
35-
from pandas.core.indexes.base import Index, InvalidIndexError, maybe_extract_name
35+
from pandas.core.indexes.base import Index, maybe_extract_name
3636
from pandas.core.ops import get_op_result_name
3737

38-
if TYPE_CHECKING:
39-
from pandas import Series
40-
4138
_num_index_shared_docs = dict()
4239

4340

@@ -383,6 +380,13 @@ def astype(self, dtype, copy=True):
383380
return Int64Index(arr)
384381
return super().astype(dtype, copy=copy)
385382

383+
# ----------------------------------------------------------------
384+
# Indexing Methods
385+
386+
@Appender(Index._should_fallback_to_positional.__doc__)
387+
def _should_fallback_to_positional(self):
388+
return False
389+
386390
@Appender(Index._convert_scalar_indexer.__doc__)
387391
def _convert_scalar_indexer(self, key, kind=None):
388392
assert kind in ["loc", "getitem", "iloc", None]
@@ -401,6 +405,8 @@ def _convert_slice_indexer(self, key: slice, kind=None):
401405
# translate to locations
402406
return self.slice_indexer(key.start, key.stop, key.step, kind=kind)
403407

408+
# ----------------------------------------------------------------
409+
404410
def _format_native_types(
405411
self, na_rep="", float_format=None, decimal=".", quoting=None, **kwargs
406412
):
@@ -416,17 +422,6 @@ def _format_native_types(
416422
)
417423
return formatter.get_result_as_array()
418424

419-
@Appender(Index.get_value.__doc__)
420-
def get_value(self, series: "Series", key):
421-
"""
422-
We always want to get an index value, never a value.
423-
"""
424-
if not is_scalar(key):
425-
raise InvalidIndexError
426-
427-
loc = self.get_loc(key)
428-
return self._get_values_for_loc(series, loc)
429-
430425
def equals(self, other) -> bool:
431426
"""
432427
Determines if two Index objects contain the same elements.

0 commit comments

Comments
 (0)