Skip to content

Commit b89f1d0

Browse files
mtsokolmroeschke
andauthored
MAINT: Adjust the codebase to the new np.array's copy keyword meaning (#57172)
* MAINT: Adjust the codebase to the new np.array copy keyword meaning * Add copy is docstring * Use asarray where possible --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 7988029 commit b89f1d0

33 files changed

+128
-58
lines changed

pandas/core/array_algos/quantile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def quantile_with_mask(
102102
interpolation=interpolation,
103103
)
104104

105-
result = np.array(result, copy=False)
105+
result = np.asarray(result)
106106
result = result.T
107107

108108
return result
@@ -201,9 +201,9 @@ def _nanpercentile(
201201
]
202202
if values.dtype.kind == "f":
203203
# preserve itemsize
204-
result = np.array(result, dtype=values.dtype, copy=False).T
204+
result = np.asarray(result, dtype=values.dtype).T
205205
else:
206-
result = np.array(result, copy=False).T
206+
result = np.asarray(result).T
207207
if (
208208
result.dtype != values.dtype
209209
and not mask.all()

pandas/core/arrays/arrow/array.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,9 @@ def __arrow_array__(self, type=None):
659659
"""Convert myself to a pyarrow ChunkedArray."""
660660
return self._pa_array
661661

662-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
662+
def __array__(
663+
self, dtype: NpDtype | None = None, copy: bool | None = None
664+
) -> np.ndarray:
663665
"""Correctly construct numpy arrays when passed to `np.asarray()`."""
664666
return self.to_numpy(dtype=dtype)
665667

pandas/core/arrays/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,10 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
725725

726726
return TimedeltaArray._from_sequence(self, dtype=dtype, copy=copy)
727727

728-
return np.array(self, dtype=dtype, copy=copy)
728+
if not copy:
729+
return np.asarray(self, dtype=dtype)
730+
else:
731+
return np.array(self, dtype=dtype, copy=copy)
729732

730733
def isna(self) -> np.ndarray | ExtensionArraySupportsAnyAll:
731734
"""

pandas/core/arrays/categorical.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,9 @@ def _validate_codes_for_dtype(cls, codes, *, dtype: CategoricalDtype) -> np.ndar
16591659
# -------------------------------------------------------------
16601660

16611661
@ravel_compat
1662-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
1662+
def __array__(
1663+
self, dtype: NpDtype | None = None, copy: bool | None = None
1664+
) -> np.ndarray:
16631665
"""
16641666
The numpy array interface.
16651667
@@ -1668,6 +1670,9 @@ def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
16681670
dtype : np.dtype or None
16691671
Specifies the the dtype for the array.
16701672
1673+
copy : bool or None, optional
1674+
Unused.
1675+
16711676
Returns
16721677
-------
16731678
numpy.array

pandas/core/arrays/datetimelike.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ def _formatter(self, boxed: bool = False) -> Callable[[object], str]:
353353
# ----------------------------------------------------------------
354354
# Array-Like / EA-Interface Methods
355355

356-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
356+
def __array__(
357+
self, dtype: NpDtype | None = None, copy: bool | None = None
358+
) -> np.ndarray:
357359
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
358360
if is_object_dtype(dtype):
359361
return np.array(list(self), dtype=object)

pandas/core/arrays/datetimes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,12 @@ def _resolution_obj(self) -> Resolution:
649649
# ----------------------------------------------------------------
650650
# Array-Like / EA-Interface Methods
651651

652-
def __array__(self, dtype=None) -> np.ndarray:
652+
def __array__(self, dtype=None, copy=None) -> np.ndarray:
653653
if dtype is None and self.tz:
654654
# The default for tz-aware is object, to preserve tz info
655655
dtype = object
656656

657-
return super().__array__(dtype=dtype)
657+
return super().__array__(dtype=dtype, copy=copy)
658658

659659
def __iter__(self) -> Iterator:
660660
"""
@@ -2421,7 +2421,7 @@ def objects_to_datetime64(
24212421
assert errors in ["raise", "coerce"]
24222422

24232423
# if str-dtype, convert
2424-
data = np.array(data, copy=False, dtype=np.object_)
2424+
data = np.asarray(data, dtype=np.object_)
24252425

24262426
result, tz_parsed = tslib.array_to_datetime(
24272427
data,

pandas/core/arrays/interval.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,9 @@ def is_non_overlapping_monotonic(self) -> bool:
15641564
# ---------------------------------------------------------------------
15651565
# Conversion
15661566

1567-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
1567+
def __array__(
1568+
self, dtype: NpDtype | None = None, copy: bool | None = None
1569+
) -> np.ndarray:
15681570
"""
15691571
Return the IntervalArray's data as a numpy array of Interval
15701572
objects (with dtype='object')

pandas/core/arrays/masked.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,9 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
594594

595595
__array_priority__ = 1000 # higher than ndarray so ops dispatch to us
596596

597-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
597+
def __array__(
598+
self, dtype: NpDtype | None = None, copy: bool | None = None
599+
) -> np.ndarray:
598600
"""
599601
the array interface, return my values
600602
We return an object array here to preserve our scalar values

pandas/core/arrays/numeric.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ def _coerce_to_data_and_mask(
160160
return values, mask, dtype, inferred_type
161161

162162
original = values
163-
values = np.array(values, copy=copy)
163+
if not copy:
164+
values = np.asarray(values)
165+
else:
166+
values = np.array(values, copy=copy)
164167
inferred_type = None
165168
if values.dtype == object or is_string_dtype(values.dtype):
166169
inferred_type = lib.infer_dtype(values, skipna=True)
@@ -169,7 +172,10 @@ def _coerce_to_data_and_mask(
169172
raise TypeError(f"{values.dtype} cannot be converted to {name}")
170173

171174
elif values.dtype.kind == "b" and checker(dtype):
172-
values = np.array(values, dtype=default_dtype, copy=copy)
175+
if not copy:
176+
values = np.asarray(values, dtype=default_dtype)
177+
else:
178+
values = np.array(values, dtype=default_dtype, copy=copy)
173179

174180
elif values.dtype.kind not in "iuf":
175181
name = dtype_cls.__name__.strip("_")
@@ -208,9 +214,9 @@ def _coerce_to_data_and_mask(
208214
inferred_type not in ["floating", "mixed-integer-float"]
209215
and not mask.any()
210216
):
211-
values = np.array(original, dtype=dtype, copy=False)
217+
values = np.asarray(original, dtype=dtype)
212218
else:
213-
values = np.array(original, dtype="object", copy=False)
219+
values = np.asarray(original, dtype="object")
214220

215221
# we copy as need to coerce here
216222
if mask.any():

pandas/core/arrays/numpy_.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ def dtype(self) -> NumpyEADtype:
150150
# ------------------------------------------------------------------------
151151
# NumPy Array Interface
152152

153-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
153+
def __array__(
154+
self, dtype: NpDtype | None = None, copy: bool | None = None
155+
) -> np.ndarray:
154156
return np.asarray(self._ndarray, dtype=dtype)
155157

156158
def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):

0 commit comments

Comments
 (0)