Skip to content

BUG: Series.loc[-1] with UInt64Index #41777

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 4 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ cdef class IndexEngine:

try:
return self.mapping.get_item(val)
except (TypeError, ValueError):
except (TypeError, ValueError, OverflowError):
# GH#41775 OverflowError e.g. if we are uint64 and val is -1
raise KeyError(val)

cdef inline _get_loc_duplicates(self, object val):
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 @@ -5413,6 +5413,11 @@ def _find_common_type_compat(self, target) -> DtypeObj:

target_dtype, _ = infer_dtype_from(target, pandas_dtype=True)
dtype = find_common_type([self.dtype, target_dtype])

if {self.dtype.kind, target_dtype.kind} == {"i", "u"}:
# See comment in Index.union about losslessness
return np.dtype("object")

if dtype.kind in ["i", "u"]:
# TODO: what about reversed with self being categorical?
if (
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
TimedeltaArray,
)
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin
import pandas.core.common as com
import pandas.core.indexes.base as ibase
from pandas.core.indexes.base import (
Index,
Expand Down Expand Up @@ -599,7 +598,7 @@ def _convert_arr_indexer(self, keyarr):
try:
return self._data._validate_listlike(keyarr, allow_object=True)
except (ValueError, TypeError):
return com.asarray_tuplesafe(keyarr)
return super()._convert_arr_indexer(keyarr)


class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin):
Expand Down
16 changes: 0 additions & 16 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
)
from pandas.core.dtypes.generic import ABCSeries

import pandas.core.common as com
from pandas.core.indexes.base import (
Index,
maybe_extract_name,
Expand Down Expand Up @@ -250,21 +249,6 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
# we will try to coerce to integers
return self._maybe_cast_indexer(label)

@doc(Index._convert_arr_indexer)
def _convert_arr_indexer(self, keyarr) -> np.ndarray:
if not is_unsigned_integer_dtype(self.dtype):
return super()._convert_arr_indexer(keyarr)

# Cast the indexer to uint64 if possible so that the values returned
# from indexing are also uint64.
dtype = None
if is_integer_dtype(keyarr) or (
lib.infer_dtype(keyarr, skipna=False) == "integer"
):
dtype = np.dtype(np.uint64)

return com.asarray_tuplesafe(keyarr, dtype=dtype)

# ----------------------------------------------------------------

@doc(Index._shallow_copy)
Expand Down
28 changes: 21 additions & 7 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,18 +1005,32 @@ def test_loc_copy_vs_view(self):
def test_loc_uint64(self):
# GH20722
# Test whether loc accept uint64 max value as index.
s = Series([1, 2], index=[np.iinfo("uint64").max - 1, np.iinfo("uint64").max])
umax = np.iinfo("uint64").max
ser = Series([1, 2], index=[umax - 1, umax])

result = s.loc[np.iinfo("uint64").max - 1]
expected = s.iloc[0]
result = ser.loc[umax - 1]
expected = ser.iloc[0]
assert result == expected

result = s.loc[[np.iinfo("uint64").max - 1]]
expected = s.iloc[[0]]
result = ser.loc[[umax - 1]]
expected = ser.iloc[[0]]
tm.assert_series_equal(result, expected)

result = s.loc[[np.iinfo("uint64").max - 1, np.iinfo("uint64").max]]
tm.assert_series_equal(result, s)
result = ser.loc[[umax - 1, umax]]
tm.assert_series_equal(result, ser)

def test_loc_uint64_disallow_negative(self):
# GH#41775
umax = np.iinfo("uint64").max
ser = Series([1, 2], index=[umax - 1, umax])

with pytest.raises(KeyError, match="-1"):
# don't wrap around
ser.loc[-1]

with pytest.raises(KeyError, match="-1"):
# don't wrap around
ser.loc[[-1]]

def test_loc_setitem_empty_append_expands_rows(self):
# GH6173, various appends to an empty dataframe
Expand Down