Skip to content

Commit 7880cf0

Browse files
authored
CLN: No need to use libindex.get_value_at (#31506)
1 parent 6d9ba2a commit 7880cf0

File tree

7 files changed

+18
-35
lines changed

7 files changed

+18
-35
lines changed

pandas/core/base.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -1027,12 +1027,10 @@ def tolist(self):
10271027
--------
10281028
numpy.ndarray.tolist
10291029
"""
1030-
if self.dtype.kind in ["m", "M"]:
1031-
return [com.maybe_box_datetimelike(x) for x in self._values]
1032-
elif is_extension_array_dtype(self._values):
1030+
if not isinstance(self._values, np.ndarray):
1031+
# check for ndarray instead of dtype to catch DTA/TDA
10331032
return list(self._values)
1034-
else:
1035-
return self._values.tolist()
1033+
return self._values.tolist()
10361034

10371035
to_list = tolist
10381036

@@ -1049,9 +1047,8 @@ def __iter__(self):
10491047
iterator
10501048
"""
10511049
# We are explicitly making element iterators.
1052-
if self.dtype.kind in ["m", "M"]:
1053-
return map(com.maybe_box_datetimelike, self._values)
1054-
elif is_extension_array_dtype(self._values):
1050+
if not isinstance(self._values, np.ndarray):
1051+
# Check type instead of dtype to catch DTA/TDA
10551052
return iter(self._values)
10561053
else:
10571054
return map(self._values.item, range(self._values.size))

pandas/core/frame.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -2900,12 +2900,8 @@ def _get_value(self, index, col, takeable: bool = False):
29002900
engine = self.index._engine
29012901

29022902
try:
2903-
if isinstance(series._values, np.ndarray):
2904-
# i.e. not EA, we can use engine
2905-
return engine.get_value(series._values, index)
2906-
else:
2907-
loc = series.index.get_loc(index)
2908-
return series._values[loc]
2903+
loc = engine.get_loc(index)
2904+
return series._values[loc]
29092905
except KeyError:
29102906
# GH 20629
29112907
if self.index.nlevels > 1:

pandas/core/indexes/base.py

-4
Original file line numberDiff line numberDiff line change
@@ -4624,10 +4624,6 @@ def _get_values_for_loc(self, series, loc):
46244624
Assumes that `series.index is self`
46254625
"""
46264626
if is_integer(loc):
4627-
if isinstance(series._values, np.ndarray):
4628-
# Since we have an ndarray and not DatetimeArray, we dont
4629-
# have to worry about a tz.
4630-
return libindex.get_value_at(series._values, loc, tz=None)
46314627
return series._values[loc]
46324628

46334629
return series.iloc[loc]

pandas/core/series.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from pandas._config import get_option
2424

25-
from pandas._libs import index as libindex, lib, properties, reshape, tslibs
25+
from pandas._libs import lib, properties, reshape, tslibs
2626
from pandas._typing import Label
2727
from pandas.compat.numpy import function as nv
2828
from pandas.util._decorators import Appender, Substitution
@@ -838,13 +838,7 @@ def _ixs(self, i: int, axis: int = 0):
838838
-------
839839
scalar (int) or Series (slice, sequence)
840840
"""
841-
842-
# dispatch to the values if we need
843-
values = self._values
844-
if isinstance(values, np.ndarray):
845-
return libindex.get_value_at(values, i)
846-
else:
847-
return values[i]
841+
return self._values[i]
848842

849843
def _slice(self, slobj: slice, axis: int = 0, kind=None) -> "Series":
850844
slobj = self.index._convert_slice_indexer(slobj, kind=kind or "getitem")
@@ -981,7 +975,7 @@ def _get_value(self, label, takeable: bool = False):
981975
scalar value
982976
"""
983977
if takeable:
984-
return com.maybe_box_datetimelike(self._values[label])
978+
return self._values[label]
985979
return self.index.get_value(self, label)
986980

987981
def __setitem__(self, key, value):

pandas/tests/indexes/period/test_indexing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -486,15 +486,17 @@ def test_get_value_datetime_hourly(self, freq):
486486
assert ser.loc[ts2] == 7
487487

488488
def test_get_value_integer(self):
489+
msg = "index 16801 is out of bounds for axis 0 with size 3"
489490
dti = pd.date_range("2016-01-01", periods=3)
490491
pi = dti.to_period("D")
491492
ser = pd.Series(range(3), index=pi)
492-
with pytest.raises(IndexError, match="index out of bounds"):
493+
with pytest.raises(IndexError, match=msg):
493494
pi.get_value(ser, 16801)
494495

496+
msg = "index 46 is out of bounds for axis 0 with size 3"
495497
pi2 = dti.to_period("Y") # duplicates, ordinals are all 46
496498
ser2 = pd.Series(range(3), index=pi2)
497-
with pytest.raises(IndexError, match="index out of bounds"):
499+
with pytest.raises(IndexError, match=msg):
498500
pi2.get_value(ser2, 46)
499501

500502
def test_is_monotonic_increasing(self):

pandas/tests/series/indexing/test_indexing.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
def test_basic_indexing():
1818
s = Series(np.random.randn(5), index=["a", "b", "a", "a", "b"])
1919

20-
msg = "index out of bounds"
20+
msg = "index 5 is out of bounds for axis 0 with size 5"
2121
with pytest.raises(IndexError, match=msg):
2222
s[5]
23-
msg = "index 5 is out of bounds for axis 0 with size 5"
2423
with pytest.raises(IndexError, match=msg):
2524
s[5] = 0
2625

@@ -29,7 +28,6 @@ def test_basic_indexing():
2928

3029
s = s.sort_index()
3130

32-
msg = r"index out of bounds|^5$"
3331
with pytest.raises(IndexError, match=msg):
3432
s[5]
3533
msg = r"index 5 is out of bounds for axis (0|1) with size 5|^5$"
@@ -165,11 +163,12 @@ def test_getitem_with_duplicates_indices(result_1, duplicate_item, expected_1):
165163

166164
def test_getitem_out_of_bounds(datetime_series):
167165
# don't segfault, GH #495
168-
msg = "index out of bounds"
166+
msg = r"index \d+ is out of bounds for axis 0 with size \d+"
169167
with pytest.raises(IndexError, match=msg):
170168
datetime_series[len(datetime_series)]
171169

172170
# GH #917
171+
msg = r"index -\d+ is out of bounds for axis 0 with size \d+"
173172
s = Series([], dtype=object)
174173
with pytest.raises(IndexError, match=msg):
175174
s[-1]

pandas/tests/series/indexing/test_numeric.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,9 @@ def test_slice_float64():
7171
def test_getitem_negative_out_of_bounds():
7272
s = Series(tm.rands_array(5, 10), index=tm.rands_array(10, 10))
7373

74-
msg = "index out of bounds"
74+
msg = "index -11 is out of bounds for axis 0 with size 10"
7575
with pytest.raises(IndexError, match=msg):
7676
s[-11]
77-
msg = "index -11 is out of bounds for axis 0 with size 10"
7877
with pytest.raises(IndexError, match=msg):
7978
s[-11] = "foo"
8079

0 commit comments

Comments
 (0)