Skip to content

Commit 1262471

Browse files
authored
PERF: construction with integer dtype (#50306)
1 parent 1613f26 commit 1262471

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

pandas/core/dtypes/cast.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1525,9 +1525,7 @@ def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray:
15251525
return result
15261526

15271527

1528-
def maybe_cast_to_integer_array(
1529-
arr: list | np.ndarray, dtype: np.dtype, copy: bool = False
1530-
) -> np.ndarray:
1528+
def maybe_cast_to_integer_array(arr: list | np.ndarray, dtype: np.dtype) -> np.ndarray:
15311529
"""
15321530
Takes any dtype and returns the casted version, raising for when data is
15331531
incompatible with integer/unsigned integer dtypes.
@@ -1538,8 +1536,6 @@ def maybe_cast_to_integer_array(
15381536
The array to cast.
15391537
dtype : np.dtype
15401538
The integer dtype to cast the array to.
1541-
copy: bool, default False
1542-
Whether to make a copy of the array before returning.
15431539
15441540
Returns
15451541
-------
@@ -1571,15 +1567,19 @@ def maybe_cast_to_integer_array(
15711567

15721568
try:
15731569
if not isinstance(arr, np.ndarray):
1574-
casted = np.array(arr, dtype=dtype, copy=copy)
1570+
casted = np.array(arr, dtype=dtype, copy=False)
15751571
else:
1576-
casted = arr.astype(dtype, copy=copy)
1572+
casted = arr.astype(dtype, copy=False)
15771573
except OverflowError as err:
15781574
raise OverflowError(
15791575
"The elements provided in the data cannot all be "
15801576
f"casted to the dtype {dtype}"
15811577
) from err
15821578

1579+
if isinstance(arr, np.ndarray) and arr.dtype == dtype:
1580+
# avoid expensive array_equal check
1581+
return casted
1582+
15831583
with warnings.catch_warnings():
15841584
warnings.filterwarnings("ignore")
15851585
if np.array_equal(arr, casted):

0 commit comments

Comments
 (0)