Skip to content

BUG: Series[uintarray] failing to raise KeyError #37495

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 12 commits into from
Oct 30, 2020
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ def test_getitem_intlist_multiindex_numeric_level(self, dtype, box):
with pytest.raises(KeyError, match="5"):
ser[key]

@pytest.mark.parametrize("dtype", [np.uint8, np.uint16, np.uint32, np.uint64])
Copy link
Contributor

Choose a reason for hiding this comment

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

can you create a fixture that is all numpy + all ea int dtypes (maybe call it INT_INDEXING or something) and use it here

Copy link
Member Author

Choose a reason for hiding this comment

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

looks like there is a fixture uint_dtype that makes sense to use here, will update

Copy link
Member Author

Choose a reason for hiding this comment

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

updated+greenish

def test_getitem_uint_array_key(self, dtype):
# GH #37218
ser = Series([1, 2, 3])
key = np.array([4], dtype=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):
Expand Down