Skip to content
13 changes: 5 additions & 8 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,12 +1027,10 @@ def tolist(self):
--------
numpy.ndarray.tolist
"""
if self.dtype.kind in ["m", "M"]:
return [com.maybe_box_datetimelike(x) for x in self._values]
elif is_extension_array_dtype(self._values):
if not isinstance(self._values, np.ndarray):
# check for ndarray instead of dtype to catch DTA/TDA
return list(self._values)
else:
return self._values.tolist()
return self._values.tolist()

to_list = tolist

Expand All @@ -1049,9 +1047,8 @@ def __iter__(self):
iterator
"""
# We are explicitly making element iterators.
if self.dtype.kind in ["m", "M"]:
return map(com.maybe_box_datetimelike, self._values)
elif is_extension_array_dtype(self._values):
if not isinstance(self._values, np.ndarray):
# Check type instead of dtype to catch DTA/TDA
return iter(self._values)
else:
return map(self._values.item, range(self._values.size))
Expand Down
8 changes: 2 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2900,12 +2900,8 @@ def _get_value(self, index, col, takeable: bool = False):
engine = self.index._engine

try:
if isinstance(series._values, np.ndarray):
# i.e. not EA, we can use engine
return engine.get_value(series._values, index)
else:
loc = series.index.get_loc(index)
return series._values[loc]
loc = engine.get_loc(index)
return series._values[loc]
except KeyError:
# GH 20629
if self.index.nlevels > 1:
Expand Down
4 changes: 0 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4620,10 +4620,6 @@ def _get_values_for_loc(self, series, loc):
Assumes that `series.index is self`
"""
if is_integer(loc):
if isinstance(series._values, np.ndarray):
# Since we have an ndarray and not DatetimeArray, we dont
# have to worry about a tz.
return libindex.get_value_at(series._values, loc, tz=None)
return series._values[loc]

return series.iloc[loc]
Expand Down
12 changes: 3 additions & 9 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from pandas._config import get_option

from pandas._libs import index as libindex, lib, properties, reshape, tslibs
from pandas._libs import lib, properties, reshape, tslibs
from pandas._typing import Label
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution
Expand Down Expand Up @@ -838,13 +838,7 @@ def _ixs(self, i: int, axis: int = 0):
-------
scalar (int) or Series (slice, sequence)
"""

# dispatch to the values if we need
values = self._values
if isinstance(values, np.ndarray):
return libindex.get_value_at(values, i)
else:
return values[i]
return self._values[i]

def _slice(self, slobj: slice, axis: int = 0, kind=None) -> "Series":
slobj = self.index._convert_slice_indexer(slobj, kind=kind or "getitem")
Expand Down Expand Up @@ -981,7 +975,7 @@ def _get_value(self, label, takeable: bool = False):
scalar value
"""
if takeable:
return com.maybe_box_datetimelike(self._values[label])
return self._values[label]
return self.index.get_value(self, label)

def __setitem__(self, key, value):
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/indexes/period/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,15 +486,17 @@ def test_get_value_datetime_hourly(self, freq):
assert ser.loc[ts2] == 7

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

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

def test_is_monotonic_increasing(self):
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
def test_basic_indexing():
s = Series(np.random.randn(5), index=["a", "b", "a", "a", "b"])

msg = "index out of bounds"
msg = "index 5 is out of bounds for axis 0 with size 5"
with pytest.raises(IndexError, match=msg):
s[5]
msg = "index 5 is out of bounds for axis 0 with size 5"
with pytest.raises(IndexError, match=msg):
s[5] = 0

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

s = s.sort_index()

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

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

# GH #917
msg = r"index -\d+ is out of bounds for axis 0 with size \d+"
s = Series([], dtype=object)
with pytest.raises(IndexError, match=msg):
s[-1]
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/series/indexing/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,9 @@ def test_slice_float64():
def test_getitem_negative_out_of_bounds():
s = Series(tm.rands_array(5, 10), index=tm.rands_array(10, 10))

msg = "index out of bounds"
msg = "index -11 is out of bounds for axis 0 with size 10"
with pytest.raises(IndexError, match=msg):
s[-11]
msg = "index -11 is out of bounds for axis 0 with size 10"
with pytest.raises(IndexError, match=msg):
s[-11] = "foo"

Expand Down