Skip to content

Commit 301f914

Browse files
mroeschkemtsokol
andauthored
Backport PR pandas-dev#57172: MAINT: Adjust the codebase to the new 's keyword meaning (pandas-dev#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

+3-3
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

+3-1
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

+4-1
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

+3-1
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

+3-1
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

+3-3
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

+3-1
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

+3-1
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

+10-4
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

+3-1
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):

pandas/core/arrays/period.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@ def __init__(
256256
raise raise_on_incompatible(values, dtype.freq)
257257
values, dtype = values._ndarray, values.dtype
258258

259-
values = np.array(values, dtype="int64", copy=copy)
259+
if not copy:
260+
values = np.asarray(values, dtype="int64")
261+
else:
262+
values = np.array(values, dtype="int64", copy=copy)
260263
if dtype is None:
261264
raise ValueError("dtype is not specified and cannot be inferred")
262265
dtype = cast(PeriodDtype, dtype)
@@ -400,7 +403,9 @@ def freq(self) -> BaseOffset:
400403
def freqstr(self) -> str:
401404
return freq_to_period_freqstr(self.freq.n, self.freq.name)
402405

403-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
406+
def __array__(
407+
self, dtype: NpDtype | None = None, copy: bool | None = None
408+
) -> np.ndarray:
404409
if dtype == "i8":
405410
return self.asi8
406411
elif dtype == bool:

pandas/core/arrays/sparse/array.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,9 @@ def from_spmatrix(cls, data: spmatrix) -> Self:
551551

552552
return cls._simple_new(arr, index, dtype)
553553

554-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
554+
def __array__(
555+
self, dtype: NpDtype | None = None, copy: bool | None = None
556+
) -> np.ndarray:
555557
fill_value = self.fill_value
556558

557559
if self.sp_index.ngaps == 0:

pandas/core/arrays/timedeltas.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,10 @@ def sequence_to_td64ns(
10721072
# This includes datetime64-dtype, see GH#23539, GH#29794
10731073
raise TypeError(f"dtype {data.dtype} cannot be converted to timedelta64[ns]")
10741074

1075-
data = np.array(data, copy=copy)
1075+
if not copy:
1076+
data = np.asarray(data)
1077+
else:
1078+
data = np.array(data, copy=copy)
10761079

10771080
assert data.dtype.kind == "m"
10781081
assert data.dtype != "m8" # i.e. not unit-less
@@ -1150,7 +1153,7 @@ def _objects_to_td64ns(data, unit=None, errors: DateTimeErrorChoices = "raise"):
11501153
higher level.
11511154
"""
11521155
# coerce Index to np.ndarray, converting string-dtype if necessary
1153-
values = np.array(data, dtype=np.object_, copy=False)
1156+
values = np.asarray(data, dtype=np.object_)
11541157

11551158
result = array_to_timedelta64(values, unit=unit, errors=errors)
11561159
return result.view("timedelta64[ns]")

pandas/core/construction.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,10 @@ def sanitize_array(
626626

627627
elif hasattr(data, "__array__"):
628628
# e.g. dask array GH#38645
629-
data = np.array(data, copy=copy)
629+
if not copy:
630+
data = np.asarray(data)
631+
else:
632+
data = np.array(data, copy=copy)
630633
return sanitize_array(
631634
data,
632635
index=index,
@@ -744,8 +747,11 @@ def _sanitize_str_dtypes(
744747
# GH#19853: If data is a scalar, result has already the result
745748
if not lib.is_scalar(data):
746749
if not np.all(isna(data)):
747-
data = np.array(data, dtype=dtype, copy=False)
748-
result = np.array(data, dtype=object, copy=copy)
750+
data = np.asarray(data, dtype=dtype)
751+
if not copy:
752+
result = np.asarray(data, dtype=object)
753+
else:
754+
result = np.array(data, dtype=object, copy=copy)
749755
return result
750756

751757

@@ -810,6 +816,8 @@ def _try_cast(
810816
# this will raise if we have e.g. floats
811817

812818
subarr = maybe_cast_to_integer_array(arr, dtype)
819+
elif not copy:
820+
subarr = np.asarray(arr, dtype=dtype)
813821
else:
814822
subarr = np.array(arr, dtype=dtype, copy=copy)
815823

pandas/core/dtypes/cast.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,10 @@ def construct_2d_arraylike_from_scalar(
15011501

15021502
# Attempt to coerce to a numpy array
15031503
try:
1504-
arr = np.array(value, dtype=dtype, copy=copy)
1504+
if not copy:
1505+
arr = np.asarray(value, dtype=dtype)
1506+
else:
1507+
arr = np.array(value, dtype=dtype, copy=copy)
15051508
except (ValueError, TypeError) as err:
15061509
raise TypeError(
15071510
f"DataFrame constructor called with incompatible data and dtype: {err}"
@@ -1651,7 +1654,7 @@ def maybe_cast_to_integer_array(arr: list | np.ndarray, dtype: np.dtype) -> np.n
16511654
"out-of-bound Python int",
16521655
DeprecationWarning,
16531656
)
1654-
casted = np.array(arr, dtype=dtype, copy=False)
1657+
casted = np.asarray(arr, dtype=dtype)
16551658
else:
16561659
with warnings.catch_warnings():
16571660
warnings.filterwarnings("ignore", category=RuntimeWarning)

pandas/core/dtypes/missing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ def infer_fill_value(val):
632632
"""
633633
if not is_list_like(val):
634634
val = [val]
635-
val = np.array(val, copy=False)
635+
val = np.asarray(val)
636636
if val.dtype.kind in "mM":
637637
return np.array("NaT", dtype=val.dtype)
638638
elif val.dtype == object:

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,7 @@ def to_numpy(
19801980
dtype = np.dtype(dtype)
19811981
result = self._mgr.as_array(dtype=dtype, copy=copy, na_value=na_value)
19821982
if result.dtype is not dtype:
1983-
result = np.array(result, dtype=dtype, copy=False)
1983+
result = np.asarray(result, dtype=dtype)
19841984

19851985
return result
19861986

pandas/core/generic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2145,7 +2145,9 @@ def empty(self) -> bool_t:
21452145
# GH#23114 Ensure ndarray.__op__(DataFrame) returns NotImplemented
21462146
__array_priority__: int = 1000
21472147

2148-
def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:
2148+
def __array__(
2149+
self, dtype: npt.DTypeLike | None = None, copy: bool_t | None = None
2150+
) -> np.ndarray:
21492151
values = self._values
21502152
arr = np.asarray(values, dtype=dtype)
21512153
if (

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ def __len__(self) -> int:
912912
"""
913913
return len(self._data)
914914

915-
def __array__(self, dtype=None) -> np.ndarray:
915+
def __array__(self, dtype=None, copy=None) -> np.ndarray:
916916
"""
917917
The array interface, return my values.
918918
"""

pandas/core/indexes/multi.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ def _values(self) -> np.ndarray:
774774
):
775775
vals = vals.astype(object)
776776

777-
vals = np.array(vals, copy=False)
777+
vals = np.asarray(vals)
778778
vals = algos.take_nd(vals, codes, fill_value=index._na_value)
779779
values.append(vals)
780780

@@ -1309,7 +1309,7 @@ def copy( # type: ignore[override]
13091309
new_index._id = self._id
13101310
return new_index
13111311

1312-
def __array__(self, dtype=None) -> np.ndarray:
1312+
def __array__(self, dtype=None, copy=None) -> np.ndarray:
13131313
"""the array interface, return my values"""
13141314
return self.values
13151315

@@ -3397,7 +3397,7 @@ def convert_indexer(start, stop, step, indexer=indexer, codes=level_codes):
33973397
locs = (level_codes >= idx.start) & (level_codes < idx.stop)
33983398
return locs
33993399

3400-
locs = np.array(level_codes == idx, dtype=bool, copy=False)
3400+
locs = np.asarray(level_codes == idx, dtype=bool)
34013401

34023402
if not locs.any():
34033403
# The label is present in self.levels[level] but unused:

pandas/core/internals/managers.py

+2
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,8 @@ def as_array(
16821682
na_value=na_value,
16831683
copy=copy,
16841684
).reshape(blk.shape)
1685+
elif not copy:
1686+
arr = np.asarray(blk.values, dtype=dtype)
16851687
else:
16861688
arr = np.array(blk.values, dtype=dtype, copy=copy)
16871689

pandas/core/series.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,9 @@ def view(self, dtype: Dtype | None = None) -> Series:
971971

972972
# ----------------------------------------------------------------------
973973
# NDArray Compat
974-
def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:
974+
def __array__(
975+
self, dtype: npt.DTypeLike | None = None, copy: bool | None = None
976+
) -> np.ndarray:
975977
"""
976978
Return the values as a NumPy array.
977979
@@ -984,6 +986,9 @@ def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:
984986
The dtype to use for the resulting NumPy array. By default,
985987
the dtype is inferred from the data.
986988
989+
copy : bool or None, optional
990+
Unused.
991+
987992
Returns
988993
-------
989994
numpy.ndarray

pandas/io/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4065,7 +4065,7 @@ def _create_axes(
40654065
if isinstance(data_converted.dtype, CategoricalDtype):
40664066
ordered = data_converted.ordered
40674067
meta = "category"
4068-
metadata = np.array(data_converted.categories, copy=False).ravel()
4068+
metadata = np.asarray(data_converted.categories).ravel()
40694069

40704070
data, dtype_name = _get_data_and_dtype_name(data_converted)
40714071

pandas/tests/arrays/integer/test_arithmetic.py

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ def test_error_invalid_values(data, all_arithmetic_operators, using_infer_string
197197
"Addition/subtraction of integers and integer-arrays with Timestamp",
198198
"has no kernel",
199199
"not implemented",
200+
"The 'out' kwarg is necessary. Use numpy.strings.multiply without it.",
200201
]
201202
)
202203
with pytest.raises(errs, match=msg):

0 commit comments

Comments
 (0)