Skip to content

Commit a5785c7

Browse files
committed
API: Update copy keyword across the codebase
1 parent 7d20c92 commit a5785c7

39 files changed

+205
-202
lines changed

numpy/_core/_add_newdocs.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -807,10 +807,11 @@
807807
a default ``dtype`` that can represent the values (by applying promotion
808808
rules when necessary.)
809809
copy : bool, optional
810-
If true (default), then the object is copied. Otherwise, a copy will
811-
only be made if ``__array__`` returns a copy, if obj is a nested
810+
If ``True`` (default), then the object is copied. If ``False``, a copy
811+
will only be made if ``__array__`` returns a copy, if obj is a nested
812812
sequence, or if a copy is needed to satisfy any of the other
813-
requirements (``dtype``, ``order``, etc.).
813+
requirements (``dtype``, ``order``, etc.). For ``False`` it raises
814+
a ``ValueError`` if a copy cannot be avoided.
814815
order : {'K', 'A', 'C', 'F'}, optional
815816
Specify the memory layout of the array. If object is not an array, the
816817
newly created array will be in C order (row major) unless 'F' is
@@ -826,7 +827,7 @@
826827
'F' F order F order
827828
===== ========= ===================================================
828829
829-
When ``copy=False`` and a copy is made for other reasons, the result is
830+
When ``copy=None`` and a copy is made for other reasons, the result is
830831
the same as if ``copy=True``, with some exceptions for 'A', see the
831832
Notes section. The default order is 'K'.
832833
subok : bool, optional
@@ -912,7 +913,7 @@
912913

913914
add_newdoc('numpy._core.multiarray', 'asarray',
914915
"""
915-
asarray(a, dtype=None, order=None, *, like=None)
916+
asarray(a, dtype=None, order=None, *, copy=None, like=None)
916917
917918
Convert the input to an array.
918919
@@ -932,12 +933,12 @@
932933
'K' (keep) preserve input order
933934
Defaults to 'K'.
934935
copy : bool, optional
935-
If true, then the object is copied. If false then the object is
936+
If ``True``, then the object is copied. If ``None`` then the object is
936937
copied only if needed, i.e. if ``__array__`` returns a copy, if obj
937938
is a nested sequence, or if a copy is needed to satisfy any of
938939
the other requirements (``dtype``, ``order``, etc.).
939-
For ``np._CopyMode.NEVER`` it raises a ``ValueError`` if a copy
940-
cannot be avoided. Default: false.
940+
For ``False`` it raises a ``ValueError`` if a copy cannot be avoided.
941+
Default: ``None``.
941942
${ARRAY_FUNCTION_LIKE}
942943
943944
.. versionadded:: 1.20.0

numpy/_core/_asarray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def require(a, dtype=None, requirements=None, *, like=None):
123123
order = 'C'
124124
requirements.remove('C')
125125

126-
arr = array(a, dtype=dtype, order=order, copy=False, subok=subok)
126+
arr = array(a, dtype=dtype, order=order, copy=None, subok=subok)
127127

128128
for prop in requirements:
129129
if not arr.flags[prop]:

numpy/_core/function_base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None,
181181
_nx.floor(y, out=y)
182182

183183
if retstep:
184-
return y.astype(dtype, copy=False), step
184+
return y.astype(dtype, copy=None), step
185185
else:
186-
return y.astype(dtype, copy=False)
186+
return y.astype(dtype, copy=None)
187187

188188

189189
def _logspace_dispatcher(start, stop, num=None, endpoint=None, base=None,
@@ -293,14 +293,14 @@ def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None,
293293
"""
294294
ndmax = np.broadcast(start, stop, base).ndim
295295
start, stop, base = (
296-
np.array(a, copy=False, subok=True, ndmin=ndmax)
296+
np.array(a, copy=None, subok=True, ndmin=ndmax)
297297
for a in (start, stop, base)
298298
)
299299
y = linspace(start, stop, num=num, endpoint=endpoint, axis=axis)
300300
base = np.expand_dims(base, axis=axis)
301301
if dtype is None:
302302
return _nx.power(base, y)
303-
return _nx.power(base, y).astype(dtype, copy=False)
303+
return _nx.power(base, y).astype(dtype, copy=None)
304304

305305

306306
def _geomspace_dispatcher(start, stop, num=None, endpoint=None, dtype=None,
@@ -463,7 +463,7 @@ def geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0):
463463
if axis != 0:
464464
result = _nx.moveaxis(result, 0, axis)
465465

466-
return result.astype(dtype, copy=False)
466+
return result.astype(dtype, copy=None)
467467

468468

469469
def _needs_add_docstring(obj):

numpy/_core/multiarray.pyi

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def array(
171171
object: _ArrayType,
172172
dtype: None = ...,
173173
*,
174-
copy: bool | _CopyMode = ...,
174+
copy: None | bool | _CopyMode = ...,
175175
order: _OrderKACF = ...,
176176
subok: L[True],
177177
ndmin: int = ...,
@@ -182,7 +182,7 @@ def array(
182182
object: _ArrayLike[_SCT],
183183
dtype: None = ...,
184184
*,
185-
copy: bool | _CopyMode = ...,
185+
copy: None | bool | _CopyMode = ...,
186186
order: _OrderKACF = ...,
187187
subok: bool = ...,
188188
ndmin: int = ...,
@@ -193,7 +193,7 @@ def array(
193193
object: object,
194194
dtype: None = ...,
195195
*,
196-
copy: bool | _CopyMode = ...,
196+
copy: None | bool | _CopyMode = ...,
197197
order: _OrderKACF = ...,
198198
subok: bool = ...,
199199
ndmin: int = ...,
@@ -204,7 +204,7 @@ def array(
204204
object: Any,
205205
dtype: _DTypeLike[_SCT],
206206
*,
207-
copy: bool | _CopyMode = ...,
207+
copy: None | bool | _CopyMode = ...,
208208
order: _OrderKACF = ...,
209209
subok: bool = ...,
210210
ndmin: int = ...,
@@ -215,7 +215,7 @@ def array(
215215
object: Any,
216216
dtype: DTypeLike,
217217
*,
218-
copy: bool | _CopyMode = ...,
218+
copy: None | bool | _CopyMode = ...,
219219
order: _OrderKACF = ...,
220220
subok: bool = ...,
221221
ndmin: int = ...,
@@ -468,6 +468,7 @@ def asarray(
468468
dtype: None = ...,
469469
order: _OrderKACF = ...,
470470
*,
471+
copy: None | bool = ...,
471472
like: None | _SupportsArrayFunc = ...,
472473
) -> NDArray[_SCT]: ...
473474
@overload
@@ -476,6 +477,7 @@ def asarray(
476477
dtype: None = ...,
477478
order: _OrderKACF = ...,
478479
*,
480+
copy: None | bool = ...,
479481
like: None | _SupportsArrayFunc = ...,
480482
) -> NDArray[Any]: ...
481483
@overload
@@ -484,6 +486,7 @@ def asarray(
484486
dtype: _DTypeLike[_SCT],
485487
order: _OrderKACF = ...,
486488
*,
489+
copy: None | bool = ...,
487490
like: None | _SupportsArrayFunc = ...,
488491
) -> NDArray[_SCT]: ...
489492
@overload
@@ -492,6 +495,7 @@ def asarray(
492495
dtype: DTypeLike,
493496
order: _OrderKACF = ...,
494497
*,
498+
copy: None | bool = ...,
495499
like: None | _SupportsArrayFunc = ...,
496500
) -> NDArray[Any]: ...
497501

numpy/_core/numeric.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def count_nonzero(a, axis=None, *, keepdims=False):
478478
if np.issubdtype(a.dtype, np.character):
479479
a_bool = a != a.dtype.type()
480480
else:
481-
a_bool = a.astype(np.bool, copy=False)
481+
a_bool = a.astype(np.bool, copy=None)
482482

483483
return a_bool.sum(axis=axis, dtype=np.intp, keepdims=keepdims)
484484

@@ -820,7 +820,7 @@ def convolve(a, v, mode='full'):
820820
array([2.5])
821821
822822
"""
823-
a, v = array(a, copy=False, ndmin=1), array(v, copy=False, ndmin=1)
823+
a, v = array(a, copy=None, ndmin=1), array(v, copy=None, ndmin=1)
824824
if (len(v) > len(a)):
825825
a, v = v, a
826826
if len(a) == 0:

numpy/_core/shape_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def _block_check_depths_match(arrays, parent_index=[]):
552552
def _atleast_nd(a, ndim):
553553
# Ensures `a` has at least `ndim` dimensions by prepending
554554
# ones to `a.shape` as necessary
555-
return array(a, ndmin=ndim, copy=False, subok=True)
555+
return array(a, ndmin=ndim, copy=None, subok=True)
556556

557557

558558
def _accumulate(values):

numpy/_core/src/multiarray/conversion_utils.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,8 @@ PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq)
228228
NPY_NO_EXPORT int
229229
PyArray_CopyConverter(PyObject *obj, NPY_COPYMODE *copymode) {
230230
if (obj == Py_None) {
231-
PyErr_SetString(PyExc_ValueError,
232-
"NoneType copy mode not allowed.");
233-
return NPY_FAIL;
231+
*copymode = NPY_COPY_IF_NEEDED;
232+
return NPY_SUCCEED;
234233
}
235234

236235
int int_copymode;

numpy/_core/src/multiarray/conversion_utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ NPY_NO_EXPORT int
1313
PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq);
1414

1515
typedef enum {
16-
NPY_COPY_IF_NEEDED = 0,
16+
NPY_COPY_NEVER = 0,
1717
NPY_COPY_ALWAYS = 1,
18-
NPY_COPY_NEVER = 2,
18+
NPY_COPY_IF_NEEDED = 2,
1919
} NPY_COPYMODE;
2020

2121
NPY_NO_EXPORT int

numpy/_core/src/multiarray/methods.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,8 @@ array_astype(PyArrayObject *self,
831831

832832
if (forcecopy == NPY_COPY_NEVER) {
833833
PyErr_SetString(PyExc_ValueError,
834-
"Unable to avoid copy while casting in never copy mode.");
834+
"Unable to avoid copy while casting in never copy mode. "
835+
"Use copy=None to copy only if necessary.");
835836
Py_DECREF(dtype);
836837
return NULL;
837838
}

numpy/_core/src/multiarray/multiarraymodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,7 +1827,7 @@ array_asanyarray(PyObject *NPY_UNUSED(ignored),
18271827
}
18281828

18291829
PyObject *res = _array_fromobject_generic(
1830-
op, dt_info.descr, dt_info.dtype, NPY_FALSE, order, NPY_TRUE, 0);
1830+
op, dt_info.descr, dt_info.dtype, NPY_COPY_IF_NEEDED, order, NPY_TRUE, 0);
18311831
Py_XDECREF(dt_info.descr);
18321832
Py_XDECREF(dt_info.dtype);
18331833
return res;
@@ -1868,7 +1868,7 @@ array_ascontiguousarray(PyObject *NPY_UNUSED(ignored),
18681868
}
18691869

18701870
PyObject *res = _array_fromobject_generic(
1871-
op, dt_info.descr, dt_info.dtype, NPY_FALSE, NPY_CORDER, NPY_FALSE,
1871+
op, dt_info.descr, dt_info.dtype, NPY_COPY_IF_NEEDED, NPY_CORDER, NPY_FALSE,
18721872
1);
18731873
Py_XDECREF(dt_info.descr);
18741874
Py_XDECREF(dt_info.dtype);
@@ -1910,7 +1910,7 @@ array_asfortranarray(PyObject *NPY_UNUSED(ignored),
19101910
}
19111911

19121912
PyObject *res = _array_fromobject_generic(
1913-
op, dt_info.descr, dt_info.dtype, NPY_FALSE, NPY_FORTRANORDER,
1913+
op, dt_info.descr, dt_info.dtype, NPY_COPY_IF_NEEDED, NPY_FORTRANORDER,
19141914
NPY_FALSE, 1);
19151915
Py_XDECREF(dt_info.descr);
19161916
Py_XDECREF(dt_info.dtype);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy