Skip to content

Commit 301f914

Browse files
mroeschkemtsokol
andauthored
Backport PR #57172: MAINT: Adjust the codebase to the new 's keyword meaning (#57740)
Co-authored-by: Mateusz Sokół <[email protected]>
1 parent 3cc5afa commit 301f914

33 files changed

+125
-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
@@ -656,7 +656,9 @@ def __arrow_array__(self, type=None):
656656
"""Convert myself to a pyarrow ChunkedArray."""
657657
return self._pa_array
658658

659-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
659+
def __array__(
660+
self, dtype: NpDtype | None = None, copy: bool | None = None
661+
) -> np.ndarray:
660662
"""Correctly construct numpy arrays when passed to `np.asarray()`."""
661663
return self.to_numpy(dtype=dtype)
662664

pandas/core/arrays/base.py

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

720720
return TimedeltaArray._from_sequence(self, dtype=dtype, copy=copy)
721721

722-
return np.array(self, dtype=dtype, copy=copy)
722+
if not copy:
723+
return np.asarray(self, dtype=dtype)
724+
else:
725+
return np.array(self, dtype=dtype, copy=copy)
723726

724727
def isna(self) -> np.ndarray | ExtensionArraySupportsAnyAll:
725728
"""

pandas/core/arrays/categorical.py

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

16381638
@ravel_compat
1639-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
1639+
def __array__(
1640+
self, dtype: NpDtype | None = None, copy: bool | None = None
1641+
) -> np.ndarray:
16401642
"""
16411643
The numpy array interface.
16421644

pandas/core/arrays/datetimelike.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,9 @@ def _formatter(self, boxed: bool = False):
351351
# ----------------------------------------------------------------
352352
# Array-Like / EA-Interface Methods
353353

354-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
354+
def __array__(
355+
self, dtype: NpDtype | None = None, copy: bool | None = None
356+
) -> np.ndarray:
355357
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
356358
if is_object_dtype(dtype):
357359
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
@@ -635,12 +635,12 @@ def _resolution_obj(self) -> Resolution:
635635
# ----------------------------------------------------------------
636636
# Array-Like / EA-Interface Methods
637637

638-
def __array__(self, dtype=None) -> np.ndarray:
638+
def __array__(self, dtype=None, copy=None) -> np.ndarray:
639639
if dtype is None and self.tz:
640640
# The default for tz-aware is object, to preserve tz info
641641
dtype = object
642642

643-
return super().__array__(dtype=dtype)
643+
return super().__array__(dtype=dtype, copy=copy)
644644

645645
def __iter__(self) -> Iterator:
646646
"""
@@ -2393,7 +2393,7 @@ def objects_to_datetime64(
23932393
assert errors in ["raise", "ignore", "coerce"]
23942394

23952395
# if str-dtype, convert
2396-
data = np.array(data, copy=False, dtype=np.object_)
2396+
data = np.asarray(data, dtype=np.object_)
23972397

23982398
result, tz_parsed = tslib.array_to_datetime(
23992399
data,

pandas/core/arrays/interval.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,9 @@ def is_non_overlapping_monotonic(self) -> bool:
15671567
# ---------------------------------------------------------------------
15681568
# Conversion
15691569

1570-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
1570+
def __array__(
1571+
self, dtype: NpDtype | None = None, copy: bool | None = None
1572+
) -> np.ndarray:
15711573
"""
15721574
Return the IntervalArray's data as a numpy array of Interval
15731575
objects (with dtype='object')

pandas/core/arrays/masked.py

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

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

596-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
596+
def __array__(
597+
self, dtype: NpDtype | None = None, copy: bool | None = None
598+
) -> np.ndarray:
597599
"""
598600
the array interface, return my values
599601
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
@@ -159,7 +159,10 @@ def _coerce_to_data_and_mask(
159159
return values, mask, dtype, inferred_type
160160

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

170173
elif values.dtype.kind == "b" and checker(dtype):
171-
values = np.array(values, dtype=default_dtype, copy=copy)
174+
if not copy:
175+
values = np.asarray(values, dtype=default_dtype)
176+
else:
177+
values = np.array(values, dtype=default_dtype, copy=copy)
172178

173179
elif values.dtype.kind not in "iuf":
174180
name = dtype_cls.__name__.strip("_")
@@ -207,9 +213,9 @@ def _coerce_to_data_and_mask(
207213
inferred_type not in ["floating", "mixed-integer-float"]
208214
and not mask.any()
209215
):
210-
values = np.array(original, dtype=dtype, copy=False)
216+
values = np.asarray(original, dtype=dtype)
211217
else:
212-
values = np.array(original, dtype="object", copy=False)
218+
values = np.asarray(original, dtype="object")
213219

214220
# we copy as need to coerce here
215221
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)