Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit96ce46d

Browse files
committed
API: Update copy keyword across the codebase
1 parent7d20c92 commit96ce46d

38 files changed

+196
-197
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-
Iftrue (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+
willonly 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-
Iftrue, then the object is copied. Iffalse 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
forpropinrequirements:
129129
ifnotarr.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
ifretstep:
184-
returny.astype(dtype,copy=False),step
184+
returny.astype(dtype,copy=None),step
185185
else:
186-
returny.astype(dtype,copy=False)
186+
returny.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
forain (start,stop,base)
298298
)
299299
y=linspace(start,stop,num=num,endpoint=endpoint,axis=axis)
300300
base=np.expand_dims(base,axis=axis)
301301
ifdtypeisNone:
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
ifaxis!=0:
464464
result=_nx.moveaxis(result,0,axis)
465465

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

468468

469469
def_needs_add_docstring(obj):

‎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
ifnp.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
returna_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
iflen(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-
returnarray(a,ndmin=ndim,copy=False,subok=True)
555+
returnarray(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_EXPORTint
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-
returnNPY_FAIL;
231+
*copymode=NPY_COPY_IF_NEEDED;
232+
returnNPY_SUCCEED;
234233
}
235234

236235
intint_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
typedefenum {
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_EXPORTint

‎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
returnNULL;
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
returnres;
@@ -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);

‎numpy/_core/tests/test_api.py‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,18 @@ def test_array_astype():
177177
assert_equal(a,b)
178178
assert_(not (aisb))
179179

180-
# copy=False parametercan sometimes skip a copy
180+
# copy=False parameterskips a copy
181181
b=a.astype('f4',copy=False)
182182
assert_(aisb)
183183

184184
# order parameter allows overriding of the memory layout,
185185
# forcing a copy if the layout is wrong
186-
b=a.astype('f4',order='F',copy=False)
186+
b=a.astype('f4',order='F',copy=None)
187187
assert_equal(a,b)
188188
assert_(not (aisb))
189189
assert_(b.flags.f_contiguous)
190190

191-
b=a.astype('f4',order='C',copy=False)
191+
b=a.astype('f4',order='C',copy=None)
192192
assert_equal(a,b)
193193
assert_(aisb)
194194
assert_(b.flags.c_contiguous)
@@ -214,12 +214,12 @@ class MyNDArray(np.ndarray):
214214
assert_(aisb)
215215

216216
# subok=True is default, and creates a subtype on a cast
217-
b=a.astype('i4',copy=False)
217+
b=a.astype('i4',copy=None)
218218
assert_equal(a,b)
219219
assert_equal(type(b),MyNDArray)
220220

221221
# subok=False never returns a subclass
222-
b=a.astype('f4',subok=False,copy=False)
222+
b=a.astype('f4',subok=False,copy=None)
223223
assert_equal(a,b)
224224
assert_(not (aisb))
225225
assert_(type(b)isnotMyNDArray)
@@ -536,9 +536,9 @@ def check_contig(a, ccontig, fcontig):
536536
check_contig(np.empty((2,2),order='F'),False,True)
537537

538538
# Check that np.array creates correct contiguous flags:
539-
check_contig(np.array(a,copy=False),False,False)
540-
check_contig(np.array(a,copy=False,order='C'),True,False)
541-
check_contig(np.array(a,ndmin=4,copy=False,order='F'),False,True)
539+
check_contig(np.array(a,copy=None),False,False)
540+
check_contig(np.array(a,copy=None,order='C'),True,False)
541+
check_contig(np.array(a,ndmin=4,copy=None,order='F'),False,True)
542542

543543
# Check slicing update of flags and :
544544
check_contig(a[0],True,True)
@@ -577,18 +577,15 @@ def test_astype_copyflag():
577577
res_false=arr.astype(np.intp,copy=False)
578578
# `res_false is arr` currently, but check `may_share_memory`.
579579
assertnp.may_share_memory(arr,res_false)
580-
res_if_needed=arr.astype(np.intp,copy=np._CopyMode.IF_NEEDED)
580+
res_if_needed=arr.astype(np.intp,copy=None)
581581
# `res_if_needed is arr` currently, but check `may_share_memory`.
582582
assertnp.may_share_memory(arr,res_if_needed)
583583

584584
res_never=arr.astype(np.intp,copy=np._CopyMode.NEVER)
585585
assertnp.may_share_memory(arr,res_never)
586586

587587
# Simple tests for when a copy is necessary:
588-
res_false=arr.astype(np.float64,copy=False)
589-
assert_array_equal(res_false,arr)
590-
res_if_needed=arr.astype(np.float64,
591-
copy=np._CopyMode.IF_NEEDED)
588+
res_if_needed=arr.astype(np.float64,copy=None)
592589
assert_array_equal(res_if_needed,arr)
593590
assert_raises(ValueError,arr.astype,np.float64,
594-
copy=np._CopyMode.NEVER)
591+
copy=False)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp