Skip to content

CLN: construction helper methods #41554

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
May 19, 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
7 changes: 2 additions & 5 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,11 +688,8 @@ def _try_cast(
if is_integer_dtype(dtype):
# this will raise if we have e.g. floats

# error: Argument 2 to "maybe_cast_to_integer_array" has incompatible type
# "Union[dtype, ExtensionDtype, None]"; expected "Union[ExtensionDtype, str,
# dtype, Type[str], Type[float], Type[int], Type[complex], Type[bool],
# Type[object]]"
maybe_cast_to_integer_array(arr, dtype) # type: ignore[arg-type]
dtype = cast(np.dtype, dtype)
maybe_cast_to_integer_array(arr, dtype)
subarr = arr
else:
subarr = maybe_cast_to_datetime(arr, dtype)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2017,7 +2017,7 @@ def maybe_cast_to_integer_array(
if is_unsigned_integer_dtype(dtype) and (arr < 0).any():
raise OverflowError("Trying to coerce negative values to unsigned integers")

if is_float_dtype(arr) or is_object_dtype(arr):
if is_float_dtype(arr.dtype) or is_object_dtype(arr.dtype):
raise ValueError("Trying to coerce float values to integers")


Expand Down
41 changes: 14 additions & 27 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6386,19 +6386,18 @@ def maybe_extract_name(name, obj, cls) -> Hashable:
return name


def _maybe_cast_data_without_dtype(subarr):
def _maybe_cast_data_without_dtype(subarr: np.ndarray) -> ArrayLike:
"""
If we have an arraylike input but no passed dtype, try to infer
a supported dtype.

Parameters
----------
subarr : np.ndarray, Index, or Series
subarr : np.ndarray[object]

Returns
-------
converted : np.ndarray or ExtensionArray
dtype : np.dtype or ExtensionDtype
np.ndarray or ExtensionArray
"""
# Runtime import needed bc IntervalArray imports Index
from pandas.core.arrays import (
Expand All @@ -6413,11 +6412,7 @@ def _maybe_cast_data_without_dtype(subarr):

if inferred == "integer":
try:
# error: Argument 3 to "_try_convert_to_int_array" has incompatible type
# "None"; expected "dtype[Any]"
data = _try_convert_to_int_array(
subarr, False, None # type: ignore[arg-type]
)
data = _try_convert_to_int_array(subarr)
return data
except ValueError:
pass
Expand Down Expand Up @@ -6463,18 +6458,13 @@ def _maybe_cast_data_without_dtype(subarr):
return subarr


def _try_convert_to_int_array(
data: np.ndarray, copy: bool, dtype: np.dtype
) -> np.ndarray:
def _try_convert_to_int_array(data: np.ndarray) -> np.ndarray:
"""
Attempt to convert an array of data into an integer array.

Parameters
----------
data : The data to convert.
copy : bool
Whether to copy the data or not.
dtype : np.dtype
data : np.ndarray[object]

Returns
-------
Expand All @@ -6484,22 +6474,19 @@ def _try_convert_to_int_array(
------
ValueError if the conversion was not successful.
"""
if not is_unsigned_integer_dtype(dtype):
# skip int64 conversion attempt if uint-like dtype is passed, as
# this could return Int64Index when UInt64Index is what's desired
try:
res = data.astype("i8", copy=False)
if (res == data).all():
return res # TODO: might still need to copy
except (OverflowError, TypeError, ValueError):
pass
try:
res = data.astype("i8", copy=False)
if (res == data).all():
return res
except (OverflowError, TypeError, ValueError):
pass

# Conversion to int64 failed (possibly due to overflow) or was skipped,
# Conversion to int64 failed (possibly due to overflow),
# so let's try now with uint64.
try:
res = data.astype("u8", copy=False)
if (res == data).all():
return res # TODO: might still need to copy
return res
except (OverflowError, TypeError, ValueError):
pass

Expand Down