diff --git a/pandas/core/base.py b/pandas/core/base.py index 9fe1af776dd2b..f3c8b50e774af 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -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 @@ -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)) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 70e440b49ae6c..35d84922925d4 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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: diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 22ba317e78e63..d91f899d7fa42 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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] diff --git a/pandas/core/series.py b/pandas/core/series.py index e5cea8ebfc914..645322401cd2d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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 @@ -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") @@ -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): diff --git a/pandas/tests/indexes/period/test_indexing.py b/pandas/tests/indexes/period/test_indexing.py index 38514594efe09..fffc4a7562306 100644 --- a/pandas/tests/indexes/period/test_indexing.py +++ b/pandas/tests/indexes/period/test_indexing.py @@ -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): diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index 18dbd22b73b35..ed05be3dedc7d 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -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 @@ -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$" @@ -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] diff --git a/pandas/tests/series/indexing/test_numeric.py b/pandas/tests/series/indexing/test_numeric.py index 3684ca00c2f17..771f5533f29d8 100644 --- a/pandas/tests/series/indexing/test_numeric.py +++ b/pandas/tests/series/indexing/test_numeric.py @@ -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"