Skip to content

Commit f19035d

Browse files
jbrockmendeljreback
authored andcommitted
REF: fix calls to Index.get_value (#31112)
1 parent 4e9ee4d commit f19035d

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

pandas/core/indexes/base.py

+19-22
Original file line numberDiff line numberDiff line change
@@ -4621,34 +4621,31 @@ def argsort(self, *args, **kwargs) -> np.ndarray:
46214621
@Appender(_index_shared_docs["get_value"] % _index_doc_kwargs)
46224622
def get_value(self, series, key):
46234623

4624+
if not is_scalar(key):
4625+
# if key is not a scalar, directly raise an error (the code below
4626+
# would convert to numpy arrays and raise later any way) - GH29926
4627+
raise InvalidIndexError(key)
4628+
46244629
# if we have something that is Index-like, then
46254630
# use this, e.g. DatetimeIndex
46264631
# Things like `Series._get_value` (via .at) pass the EA directly here.
46274632
s = extract_array(series, extract_numpy=True)
46284633
if isinstance(s, ExtensionArray):
4629-
if is_scalar(key):
4630-
# GH 20882, 21257
4631-
# First try to convert the key to a location
4632-
# If that fails, raise a KeyError if an integer
4633-
# index, otherwise, see if key is an integer, and
4634-
# try that
4635-
try:
4636-
iloc = self.get_loc(key)
4637-
return s[iloc]
4638-
except KeyError:
4639-
if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
4640-
raise
4641-
elif is_integer(key):
4642-
return s[key]
4643-
else:
4644-
# if key is not a scalar, directly raise an error (the code below
4645-
# would convert to numpy arrays and raise later any way) - GH29926
4646-
raise InvalidIndexError(key)
4647-
4648-
s = com.values_from_object(series)
4649-
k = com.values_from_object(key)
4634+
# GH 20882, 21257
4635+
# First try to convert the key to a location
4636+
# If that fails, raise a KeyError if an integer
4637+
# index, otherwise, see if key is an integer, and
4638+
# try that
4639+
try:
4640+
iloc = self.get_loc(key)
4641+
return s[iloc]
4642+
except KeyError:
4643+
if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
4644+
raise
4645+
elif is_integer(key):
4646+
return s[key]
46504647

4651-
k = self._convert_scalar_indexer(k, kind="getitem")
4648+
k = self._convert_scalar_indexer(key, kind="getitem")
46524649
try:
46534650
return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
46544651
except KeyError as e1:

pandas/core/indexes/category.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,8 @@ def get_value(self, series: AnyArrayLike, key: Any):
503503
Any
504504
The element of the series at the position indicated by the key
505505
"""
506+
k = key
506507
try:
507-
k = com.values_from_object(key)
508508
k = self._convert_scalar_indexer(k, kind="getitem")
509509
indexer = self.get_loc(k)
510510
return series.take([indexer])[0]

pandas/core/indexes/numeric.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import TYPE_CHECKING
2+
13
import numpy as np
24

35
from pandas._libs import index as libindex, lib
@@ -38,6 +40,9 @@
3840
)
3941
from pandas.core.ops import get_op_result_name
4042

43+
if TYPE_CHECKING:
44+
from pandas import Series
45+
4146
_num_index_shared_docs = dict()
4247

4348

@@ -438,17 +443,18 @@ def _format_native_types(
438443
)
439444
return formatter.get_result_as_array()
440445

441-
def get_value(self, series, key):
446+
def get_value(self, series: "Series", key):
442447
"""
443448
We always want to get an index value, never a value.
444449
"""
445450
if not is_scalar(key):
446451
raise InvalidIndexError
447452

448-
k = com.values_from_object(key)
449-
loc = self.get_loc(k)
450-
new_values = com.values_from_object(series)[loc]
453+
loc = self.get_loc(key)
454+
if not is_scalar(loc):
455+
return series.iloc[loc]
451456

457+
new_values = series._values[loc]
452458
return new_values
453459

454460
def equals(self, other) -> bool:

pandas/core/series.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,10 @@ def _slice(self, slobj: slice, axis: int = 0, kind=None) -> "Series":
808808

809809
def __getitem__(self, key):
810810
key = com.apply_if_callable(key, self)
811+
812+
if key is Ellipsis:
813+
return self
814+
811815
try:
812816
result = self.index.get_value(self, key)
813817

@@ -830,8 +834,6 @@ def __getitem__(self, key):
830834
if isinstance(key, tuple) and isinstance(self.index, MultiIndex):
831835
# kludge
832836
pass
833-
elif key is Ellipsis:
834-
return self
835837
elif com.is_bool_indexer(key):
836838
pass
837839
else:
@@ -939,7 +941,7 @@ def _get_value(self, label, takeable: bool = False):
939941
"""
940942
if takeable:
941943
return com.maybe_box_datetimelike(self._values[label])
942-
return self.index.get_value(self._values, label)
944+
return self.index.get_value(self, label)
943945

944946
def __setitem__(self, key, value):
945947
key = com.apply_if_callable(key, self)

0 commit comments

Comments
 (0)