From 362cfe0c7444374abc294a63fbc447cf2ad412ee Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sat, 10 Dec 2022 21:46:43 +0100 Subject: [PATCH] BUG: loc for ea index raising with oob slice end --- doc/source/whatsnew/v2.0.0.rst | 1 + pandas/core/indexes/base.py | 5 +++++ pandas/tests/series/indexing/test_indexing.py | 9 +++++++++ 3 files changed, 15 insertions(+) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index d6e0bb2ae0830..1e797ed9eb900 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -750,6 +750,7 @@ Indexing ^^^^^^^^ - Bug in :meth:`DataFrame.reindex` filling with wrong values when indexing columns and index for ``uint`` dtypes (:issue:`48184`) - Bug in :meth:`DataFrame.loc` coercing dtypes when setting values with a list indexer (:issue:`49159`) +- Bug in :meth:`Series.loc` raising error for out of bounds end of slice indexer (:issue:`50161`) - Bug in :meth:`DataFrame.loc` raising ``ValueError`` with ``bool`` indexer and :class:`MultiIndex` (:issue:`47687`) - Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` when right hand side is :class:`DataFrame` with :class:`MultiIndex` columns (:issue:`49121`) - Bug in :meth:`DataFrame.reindex` casting dtype to ``object`` when :class:`DataFrame` has single extension array column when re-indexing ``columns`` and ``index`` (:issue:`48190`) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 8cbe6177447c1..527b2613ea6fc 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6199,6 +6199,11 @@ def _maybe_cast_slice_bound(self, label, side: str_t): # We are a plain index here (sub-class override this method if they # wish to have special treatment for floats/ints, e.g. Float64Index and # datetimelike Indexes + # Special case numeric EA Indexes, since they are not handled by NumericIndex + + if is_extension_array_dtype(self.dtype) and is_numeric_dtype(self.dtype): + return self._maybe_cast_indexer(label) + # reject them, if index does not contain label if (is_float(label) or is_integer(label)) and label not in self: self._raise_invalid_indexer("slice", label) diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index 4efc14be3eb7e..387272eb807e8 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -10,6 +10,7 @@ from pandas import ( NA, DataFrame, + Index, IndexSlice, MultiIndex, Series, @@ -366,6 +367,14 @@ def test_loc_setitem_nested_data_enlargement(): tm.assert_series_equal(ser, expected) +def test_loc_ea_numeric_index_oob_slice_end(): + # GH#50161 + ser = Series(1, index=Index([0, 1, 2], dtype="Int64")) + result = ser.loc[2:3] + expected = Series(1, index=Index([2], dtype="Int64")) + tm.assert_series_equal(result, expected) + + def test_getitem_bool_int_key(): # GH#48653 ser = Series({True: 1, False: 0})