Skip to content

BUG: loc for ea index raising with oob slice end #50170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,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`)
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6198,6 +6198,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)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pandas import (
NA,
DataFrame,
Index,
IndexSlice,
MultiIndex,
Series,
Expand Down Expand Up @@ -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():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test for a pyarrow-backed numeric type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would fail, int64[pyarrow] returns false in is_numeric_dtype. Is this expected? Index.is_numeric will be deprecated, so should not use that either

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. I think all the dtype checkers should become pyarrow aware eventually but okay not to tackle that in this PR

# 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})
Expand Down