Skip to content

Commit 3da778c

Browse files
authored
BUG: loc for ea index raising with oob slice end (#50170)
1 parent 8117a55 commit 3da778c

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ Indexing
836836
- Bug in :meth:`DataFrame.__setitem__` raising when indexer is a :class:`DataFrame` with ``boolean`` dtype (:issue:`47125`)
837837
- Bug in :meth:`DataFrame.reindex` filling with wrong values when indexing columns and index for ``uint`` dtypes (:issue:`48184`)
838838
- Bug in :meth:`DataFrame.loc` coercing dtypes when setting values with a list indexer (:issue:`49159`)
839+
- Bug in :meth:`Series.loc` raising error for out of bounds end of slice indexer (:issue:`50161`)
839840
- Bug in :meth:`DataFrame.loc` raising ``ValueError`` with ``bool`` indexer and :class:`MultiIndex` (:issue:`47687`)
840841
- Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` when right hand side is :class:`DataFrame` with :class:`MultiIndex` columns (:issue:`49121`)
841842
- 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`)

pandas/core/indexes/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6158,6 +6158,11 @@ def _maybe_cast_slice_bound(self, label, side: str_t):
61586158
# We are a plain index here (sub-class override this method if they
61596159
# wish to have special treatment for floats/ints, e.g. Float64Index and
61606160
# datetimelike Indexes
6161+
# Special case numeric EA Indexes, since they are not handled by NumericIndex
6162+
6163+
if is_extension_array_dtype(self.dtype) and is_numeric_dtype(self.dtype):
6164+
return self._maybe_cast_indexer(label)
6165+
61616166
# reject them, if index does not contain label
61626167
if (is_float(label) or is_integer(label)) and label not in self:
61636168
self._raise_invalid_indexer("slice", label)

pandas/tests/series/indexing/test_indexing.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pandas import (
1111
NA,
1212
DataFrame,
13+
Index,
1314
IndexSlice,
1415
MultiIndex,
1516
Series,
@@ -366,6 +367,14 @@ def test_loc_setitem_nested_data_enlargement():
366367
tm.assert_series_equal(ser, expected)
367368

368369

370+
def test_loc_ea_numeric_index_oob_slice_end():
371+
# GH#50161
372+
ser = Series(1, index=Index([0, 1, 2], dtype="Int64"))
373+
result = ser.loc[2:3]
374+
expected = Series(1, index=Index([2], dtype="Int64"))
375+
tm.assert_series_equal(result, expected)
376+
377+
369378
def test_getitem_bool_int_key():
370379
# GH#48653
371380
ser = Series({True: 1, False: 0})

0 commit comments

Comments
 (0)