diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 812af544ed9d8..312a850ec3fd1 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -439,6 +439,7 @@ Indexing - Bug in :meth:`DataFrame.__getitem__` and :meth:`DataFrame.loc.__getitem__` with :class:`IntervalIndex` columns and a numeric indexer (:issue:`26490`) - Bug in :meth:`Series.loc.__getitem__` with a non-unique :class:`MultiIndex` and an empty-list indexer (:issue:`13691`) - Bug in indexing on a :class:`Series` or :class:`DataFrame` with a :class:`MultiIndex` with a level named "0" (:issue:`37194`) +- Bug in :meth:`Series.__getitem__` when using an unsigned integer array as an indexer giving incorrect results or segfaulting instead of raising ``KeyError`` (:issue:`37218`) Missing ^^^^^^^ diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 74ec892d5f8a0..3afcb0fa343d5 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -17,9 +17,9 @@ ensure_python_int, is_float, is_integer, - is_integer_dtype, is_list_like, is_scalar, + is_signed_integer_dtype, is_timedelta64_dtype, ) from pandas.core.dtypes.generic import ABCTimedeltaIndex @@ -369,7 +369,7 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None): start, stop, step = reverse.start, reverse.stop, reverse.step target_array = np.asarray(target) - if not (is_integer_dtype(target_array) and target_array.ndim == 1): + if not (is_signed_integer_dtype(target_array) and target_array.ndim == 1): # checks/conversions/roundings are delegated to general method return super().get_indexer(target, method=method, tolerance=tolerance) diff --git a/pandas/tests/series/indexing/test_getitem.py b/pandas/tests/series/indexing/test_getitem.py index 9f6aab823c3ad..ab34423fa7acd 100644 --- a/pandas/tests/series/indexing/test_getitem.py +++ b/pandas/tests/series/indexing/test_getitem.py @@ -147,6 +147,16 @@ def test_getitem_intlist_multiindex_numeric_level(self, dtype, box): with pytest.raises(KeyError, match="5"): ser[key] + def test_getitem_uint_array_key(self, uint_dtype): + # GH #37218 + ser = Series([1, 2, 3]) + key = np.array([4], dtype=uint_dtype) + + with pytest.raises(KeyError, match="4"): + ser[key] + with pytest.raises(KeyError, match="4"): + ser.loc[key] + class TestGetitemBooleanMask: def test_getitem_boolean(self, string_series):