Skip to content

Commit 213a00d

Browse files
jbrockmendelJulianWgs
authored andcommitted
CLN: construction helper methods (pandas-dev#41554)
1 parent 3c703b3 commit 213a00d

File tree

3 files changed

+17
-33
lines changed

3 files changed

+17
-33
lines changed

pandas/core/construction.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,8 @@ def _try_cast(
695695
if is_integer_dtype(dtype):
696696
# this will raise if we have e.g. floats
697697

698-
# error: Argument 2 to "maybe_cast_to_integer_array" has incompatible type
699-
# "Union[dtype, ExtensionDtype, None]"; expected "Union[ExtensionDtype, str,
700-
# dtype, Type[str], Type[float], Type[int], Type[complex], Type[bool],
701-
# Type[object]]"
702-
maybe_cast_to_integer_array(arr, dtype) # type: ignore[arg-type]
698+
dtype = cast(np.dtype, dtype)
699+
maybe_cast_to_integer_array(arr, dtype)
703700
subarr = arr
704701
else:
705702
subarr = maybe_cast_to_datetime(arr, dtype)

pandas/core/dtypes/cast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,7 @@ def maybe_cast_to_integer_array(
20262026
if is_unsigned_integer_dtype(dtype) and (arr < 0).any():
20272027
raise OverflowError("Trying to coerce negative values to unsigned integers")
20282028

2029-
if is_float_dtype(arr) or is_object_dtype(arr):
2029+
if is_float_dtype(arr.dtype) or is_object_dtype(arr.dtype):
20302030
raise ValueError("Trying to coerce float values to integers")
20312031

20322032

pandas/core/indexes/base.py

+14-27
Original file line numberDiff line numberDiff line change
@@ -6386,19 +6386,18 @@ def maybe_extract_name(name, obj, cls) -> Hashable:
63866386
return name
63876387

63886388

6389-
def _maybe_cast_data_without_dtype(subarr):
6389+
def _maybe_cast_data_without_dtype(subarr: np.ndarray) -> ArrayLike:
63906390
"""
63916391
If we have an arraylike input but no passed dtype, try to infer
63926392
a supported dtype.
63936393
63946394
Parameters
63956395
----------
6396-
subarr : np.ndarray, Index, or Series
6396+
subarr : np.ndarray[object]
63976397
63986398
Returns
63996399
-------
6400-
converted : np.ndarray or ExtensionArray
6401-
dtype : np.dtype or ExtensionDtype
6400+
np.ndarray or ExtensionArray
64026401
"""
64036402
# Runtime import needed bc IntervalArray imports Index
64046403
from pandas.core.arrays import (
@@ -6413,11 +6412,7 @@ def _maybe_cast_data_without_dtype(subarr):
64136412

64146413
if inferred == "integer":
64156414
try:
6416-
# error: Argument 3 to "_try_convert_to_int_array" has incompatible type
6417-
# "None"; expected "dtype[Any]"
6418-
data = _try_convert_to_int_array(
6419-
subarr, False, None # type: ignore[arg-type]
6420-
)
6415+
data = _try_convert_to_int_array(subarr)
64216416
return data
64226417
except ValueError:
64236418
pass
@@ -6463,18 +6458,13 @@ def _maybe_cast_data_without_dtype(subarr):
64636458
return subarr
64646459

64656460

6466-
def _try_convert_to_int_array(
6467-
data: np.ndarray, copy: bool, dtype: np.dtype
6468-
) -> np.ndarray:
6461+
def _try_convert_to_int_array(data: np.ndarray) -> np.ndarray:
64696462
"""
64706463
Attempt to convert an array of data into an integer array.
64716464
64726465
Parameters
64736466
----------
6474-
data : The data to convert.
6475-
copy : bool
6476-
Whether to copy the data or not.
6477-
dtype : np.dtype
6467+
data : np.ndarray[object]
64786468
64796469
Returns
64806470
-------
@@ -6484,22 +6474,19 @@ def _try_convert_to_int_array(
64846474
------
64856475
ValueError if the conversion was not successful.
64866476
"""
6487-
if not is_unsigned_integer_dtype(dtype):
6488-
# skip int64 conversion attempt if uint-like dtype is passed, as
6489-
# this could return Int64Index when UInt64Index is what's desired
6490-
try:
6491-
res = data.astype("i8", copy=False)
6492-
if (res == data).all():
6493-
return res # TODO: might still need to copy
6494-
except (OverflowError, TypeError, ValueError):
6495-
pass
6477+
try:
6478+
res = data.astype("i8", copy=False)
6479+
if (res == data).all():
6480+
return res
6481+
except (OverflowError, TypeError, ValueError):
6482+
pass
64966483

6497-
# Conversion to int64 failed (possibly due to overflow) or was skipped,
6484+
# Conversion to int64 failed (possibly due to overflow),
64986485
# so let's try now with uint64.
64996486
try:
65006487
res = data.astype("u8", copy=False)
65016488
if (res == data).all():
6502-
return res # TODO: might still need to copy
6489+
return res
65036490
except (OverflowError, TypeError, ValueError):
65046491
pass
65056492

0 commit comments

Comments
 (0)