Skip to content

BUG: clean_index_list handle uint64 case #41784

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 1 commit into from
Jun 2, 2021
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
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def maybe_indices_to_slice(
) -> slice | np.ndarray: ... # np.ndarray[np.uint8]

def clean_index_list(obj: list) -> tuple[
list | np.ndarray, # np.ndarray[object] | np.ndarray[np.int64]
list | np.ndarray, # np.ndarray[object | np.int64 | np.uint64]
bool,
]: ...

Expand Down
28 changes: 20 additions & 8 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -747,10 +747,14 @@ def clean_index_list(obj: list):
object val
bint all_arrays = True

# First check if we have a list of arraylikes, in which case we will
# pass them to MultiIndex.from_arrays
for i in range(n):
val = obj[i]
if not (isinstance(val, list) or
util.is_array(val) or hasattr(val, '_data')):
# TODO: EA?
# exclude tuples, frozensets as they may be contained in an Index
all_arrays = False
break

Expand All @@ -762,11 +766,21 @@ def clean_index_list(obj: list):
if inferred in ['string', 'bytes', 'mixed', 'mixed-integer']:
return np.asarray(obj, dtype=object), 0
elif inferred in ['integer']:
# TODO: we infer an integer but it *could* be a uint64
try:
return np.asarray(obj, dtype='int64'), 0
except OverflowError:
return np.asarray(obj, dtype='object'), 0
# we infer an integer but it *could* be a uint64

arr = np.asarray(obj)
if arr.dtype.kind not in ["i", "u"]:
# eg [0, uint64max] gets cast to float64,
# but then we know we have either uint64 or object
if (arr < 0).any():
# TODO: similar to maybe_cast_to_integer_array
return np.asarray(obj, dtype="object"), 0

# GH#35481
guess = np.asarray(obj, dtype="uint64")
return guess, 0

return arr, 0

return np.asarray(obj), 0

Expand Down Expand Up @@ -1552,9 +1566,7 @@ def infer_dtype(value: object, skipna: bool = True) -> str:

for i in range(n):
val = values[i]
if (util.is_integer_object(val) and
not util.is_timedelta64_object(val) and
not util.is_datetime64_object(val)):
if util.is_integer_object(val):
return "mixed-integer"

return "mixed"
Expand Down
28 changes: 6 additions & 22 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6299,27 +6299,18 @@ def ensure_index(index_like: AnyArrayLike | Sequence, copy: bool = False) -> Ind
if copy:
index_like = index_like.copy()
return index_like
if hasattr(index_like, "name"):
# https://github.com/python/mypy/issues/1424
# error: Item "ExtensionArray" of "Union[ExtensionArray,
# Sequence[Any]]" has no attribute "name"
# error: Item "Sequence[Any]" of "Union[ExtensionArray, Sequence[Any]]"
# has no attribute "name"
# error: "Sequence[Any]" has no attribute "name"
# error: Item "Sequence[Any]" of "Union[Series, Sequence[Any]]" has no
# attribute "name"
# error: Item "Sequence[Any]" of "Union[Any, Sequence[Any]]" has no
# attribute "name"
name = index_like.name # type: ignore[union-attr, attr-defined]

if isinstance(index_like, ABCSeries):
name = index_like.name
return Index(index_like, name=name, copy=copy)

if is_iterator(index_like):
index_like = list(index_like)

# must check for exactly list here because of strict type
# check in clean_index_list
if isinstance(index_like, list):
if type(index_like) != list:
if type(index_like) is not list:
# must check for exactly list here because of strict type
# check in clean_index_list
index_like = list(index_like)

converted, all_arrays = lib.clean_index_list(index_like)
Expand All @@ -6329,13 +6320,6 @@ def ensure_index(index_like: AnyArrayLike | Sequence, copy: bool = False) -> Ind

return MultiIndex.from_arrays(converted)
else:
if isinstance(converted, np.ndarray) and converted.dtype == np.int64:
# Check for overflows if we should actually be uint64
# xref GH#35481
alt = np.asarray(index_like)
if alt.dtype == np.uint64:
converted = alt

index_like = converted
else:
# clean_index_list does the equivalent of copying
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,9 @@ def _setitem_with_indexer_missing(self, indexer, value):
# e.g. 0.0 -> 0
# GH#12246
if index.is_unique:
new_indexer = index.get_indexer([new_index[-1]])
# pass new_index[-1:] instead if [new_index[-1]]
# so that we retain dtype
new_indexer = index.get_indexer(new_index[-1:])
if (new_indexer != -1).any():
# We get only here with loc, so can hard code
return self._setitem_with_indexer(new_indexer, value, "loc")
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/libs/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,15 @@ def test_no_default_pickle():
# GH#40397
obj = tm.round_trip_pickle(lib.no_default)
assert obj is lib.no_default


def test_clean_index_list():
# with both 0 and a large-uint64, np.array will infer to float64
# https://github.com/numpy/numpy/issues/19146
# but a more accurate choice would be uint64
values = [0, np.iinfo(np.uint64).max]

result, _ = lib.clean_index_list(values)

expected = np.array(values, dtype="uint64")
tm.assert_numpy_array_equal(result, expected, check_dtype=True)