@@ -1525,9 +1525,7 @@ def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray:
1525
1525
return result
1526
1526
1527
1527
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 :
1531
1529
"""
1532
1530
Takes any dtype and returns the casted version, raising for when data is
1533
1531
incompatible with integer/unsigned integer dtypes.
@@ -1538,8 +1536,6 @@ def maybe_cast_to_integer_array(
1538
1536
The array to cast.
1539
1537
dtype : np.dtype
1540
1538
The integer dtype to cast the array to.
1541
- copy: bool, default False
1542
- Whether to make a copy of the array before returning.
1543
1539
1544
1540
Returns
1545
1541
-------
@@ -1571,15 +1567,19 @@ def maybe_cast_to_integer_array(
1571
1567
1572
1568
try :
1573
1569
if not isinstance (arr , np .ndarray ):
1574
- casted = np .array (arr , dtype = dtype , copy = copy )
1570
+ casted = np .array (arr , dtype = dtype , copy = False )
1575
1571
else :
1576
- casted = arr .astype (dtype , copy = copy )
1572
+ casted = arr .astype (dtype , copy = False )
1577
1573
except OverflowError as err :
1578
1574
raise OverflowError (
1579
1575
"The elements provided in the data cannot all be "
1580
1576
f"casted to the dtype { dtype } "
1581
1577
) from err
1582
1578
1579
+ if isinstance (arr , np .ndarray ) and arr .dtype == dtype :
1580
+ # avoid expensive array_equal check
1581
+ return casted
1582
+
1583
1583
with warnings .catch_warnings ():
1584
1584
warnings .filterwarnings ("ignore" )
1585
1585
if np .array_equal (arr , casted ):
0 commit comments