diff --git a/pandas/core/construction.py b/pandas/core/construction.py index f3133480108a6..964575ba47d7b 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -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) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 46dc97214e2f6..b94b6965cc747 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -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") diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 9f0a80ba0f5c7..96556df73ffaf 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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 ( @@ -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 @@ -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 ------- @@ -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