Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07db5be

Browse files
authored
Merge pull request #24682 from mtsokol/ndarray-class-cleanup-part-1
API: Remove `ptp`, `itemset` and `newbyteorder` from `np.ndarray` class
2 parents c64da5f + 1646e7a commit 07db5be

29 files changed

+158
-414
lines changed

doc/source/reference/arrays.ndarray.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ For the following methods there are also corresponding functions in
273273
:func:`clip`, :func:`compress`, :func:`copy`, :func:`cumprod`,
274274
:func:`cumsum`, :func:`diagonal`, :func:`imag`, :func:`max <amax>`,
275275
:func:`mean`, :func:`min <amin>`, :func:`nonzero`, :func:`partition`,
276-
:func:`prod`, :func:`ptp`, :func:`put`, :func:`ravel`, :func:`real`,
276+
:func:`prod`, :func:`put`, :func:`ravel`, :func:`real`,
277277
:func:`repeat`, :func:`reshape`, :func:`round <around>`,
278278
:func:`searchsorted`, :func:`sort`, :func:`squeeze`, :func:`std`,
279279
:func:`sum`, :func:`swapaxes`, :func:`take`, :func:`trace`,
@@ -287,7 +287,6 @@ Array conversion
287287

288288
ndarray.item
289289
ndarray.tolist
290-
ndarray.itemset
291290
ndarray.tostring
292291
ndarray.tobytes
293292
ndarray.tofile
@@ -410,7 +409,6 @@ be performed.
410409
ndarray.argmax
411410
ndarray.min
412411
ndarray.argmin
413-
ndarray.ptp
414412
ndarray.clip
415413
ndarray.conj
416414
ndarray.round

doc/source/reference/c-api/array.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,8 +1994,7 @@ Calculation
19941994
.. c:function:: PyObject* PyArray_Ptp( \
19951995
PyArrayObject* self, int axis, PyArrayObject* out)
19961996
1997-
Equivalent to :meth:`ndarray.ptp<numpy.ndarray.ptp>` (*self*, *axis*). Return the difference
1998-
between the largest element of *self* along *axis* and the
1997+
Return the difference between the largest element of *self* along *axis* and the
19991998
smallest element of *self* along *axis*. When the result is a single
20001999
element, returns a numpy scalar instead of an ndarray.
20012000

doc/source/user/byteswapping.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ underlying memory it is looking at:
7979

8080
* Change the byte-ordering information in the array dtype so that it
8181
interprets the underlying data as being in a different byte order.
82-
This is the role of ``arr.newbyteorder()``
82+
This is the role of ``arr.view(arr.dtype.newbyteorder())``
8383
* Change the byte-ordering of the underlying data, leaving the dtype
8484
interpretation as it was. This is what ``arr.byteswap()`` does.
8585

@@ -104,7 +104,7 @@ We make something where they don't match:
104104
The obvious fix for this situation is to change the dtype so it gives
105105
the correct endianness:
106106

107-
>>> fixed_end_dtype_arr = wrong_end_dtype_arr.newbyteorder()
107+
>>> fixed_end_dtype_arr = wrong_end_dtype_arr.view(np.dtype('<i2').newbyteorder())
108108
>>> fixed_end_dtype_arr[0]
109109
1
110110

@@ -137,7 +137,8 @@ to have the opposite byte order in memory, and you want the dtype to
137137
match so the array values make sense. In this case you just do both of
138138
the previous operations:
139139

140-
>>> swapped_end_arr = big_end_arr.byteswap().newbyteorder()
140+
>>> swapped_end_arr = big_end_arr.byteswap()
141+
>>> swapped_end_arr = swapped_end_arr.view(swapped_end_arr.dtype.newbyteorder())
141142
>>> swapped_end_arr[0]
142143
1
143144
>>> swapped_end_arr.tobytes() == big_end_buffer

numpy/__init__.pyi

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,11 +1265,6 @@ class _ArrayOrScalarCommon:
12651265
where: _ArrayLikeBool_co = ...,
12661266
) -> _NdArraySubClass: ...
12671267

1268-
def newbyteorder(
1269-
self: _ArraySelf,
1270-
__new_order: _ByteOrder = ...,
1271-
) -> _ArraySelf: ...
1272-
12731268
@overload
12741269
def prod(
12751270
self,
@@ -1291,21 +1286,6 @@ class _ArrayOrScalarCommon:
12911286
where: _ArrayLikeBool_co = ...,
12921287
) -> _NdArraySubClass: ...
12931288

1294-
@overload
1295-
def ptp(
1296-
self,
1297-
axis: None | _ShapeLike = ...,
1298-
out: None = ...,
1299-
keepdims: bool = ...,
1300-
) -> Any: ...
1301-
@overload
1302-
def ptp(
1303-
self,
1304-
axis: None | _ShapeLike = ...,
1305-
out: _NdArraySubClass = ...,
1306-
keepdims: bool = ...,
1307-
) -> _NdArraySubClass: ...
1308-
13091289
@overload
13101290
def round(
13111291
self: _ArraySelf,
@@ -1565,11 +1545,6 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
15651545
/,
15661546
) -> _T: ...
15671547

1568-
@overload
1569-
def itemset(self, value: Any, /) -> None: ...
1570-
@overload
1571-
def itemset(self, item: _ShapeLike, value: Any, /) -> None: ...
1572-
15731548
@overload
15741549
def resize(self, new_shape: _ShapeLike, /, *, refcheck: bool = ...) -> None: ...
15751550
@overload

numpy/_expired_attrs_2_0.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"maximum_sctype": "",
4545
"obj2sctype": "",
4646
"sctype2char": "",
47-
"sctypeDict": "",
4847
"sctypes": "",
4948
"issubsctype": "Use `np.issubdtype` instead.",
5049
"set_string_function":

numpy/core/_add_newdocs.py

Lines changed: 3 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,14 +3226,14 @@
32263226
>>> A.byteswap()
32273227
array([b'ceg', b'fac'], dtype='|S3')
32283228
3229-
``A.newbyteorder().byteswap()`` produces an array with the same values
3230-
but different representation in memory
3229+
``A.view(A.dtype.newbyteorder()).byteswap()`` produces an array with
3230+
the same values but different representation in memory
32313231
32323232
>>> A = np.array([1, 2, 3])
32333233
>>> A.view(np.uint8)
32343234
array([1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0,
32353235
0, 0], dtype=uint8)
3236-
>>> A.newbyteorder().byteswap(inplace=True)
3236+
>>> A.view(A.dtype.newbyteorder()).byteswap(inplace=True)
32373237
array([1, 2, 3])
32383238
>>> A.view(np.uint8)
32393239
array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0,
@@ -3630,53 +3630,6 @@
36303630
"""))
36313631

36323632

3633-
add_newdoc('numpy.core.multiarray', 'ndarray', ('itemset',
3634-
"""
3635-
a.itemset(*args)
3636-
3637-
Insert scalar into an array (scalar is cast to array's dtype, if possible)
3638-
3639-
There must be at least 1 argument, and define the last argument
3640-
as *item*. Then, ``a.itemset(*args)`` is equivalent to but faster
3641-
than ``a[args] = item``. The item should be a scalar value and `args`
3642-
must select a single item in the array `a`.
3643-
3644-
Parameters
3645-
----------
3646-
\\*args : Arguments
3647-
If one argument: a scalar, only used in case `a` is of size 1.
3648-
If two arguments: the last argument is the value to be set
3649-
and must be a scalar, the first argument specifies a single array
3650-
element location. It is either an int or a tuple.
3651-
3652-
Notes
3653-
-----
3654-
Compared to indexing syntax, `itemset` provides some speed increase
3655-
for placing a scalar into a particular location in an `ndarray`,
3656-
if you must do this. However, generally this is discouraged:
3657-
among other problems, it complicates the appearance of the code.
3658-
Also, when using `itemset` (and `item`) inside a loop, be sure
3659-
to assign the methods to a local variable to avoid the attribute
3660-
look-up at each loop iteration.
3661-
3662-
Examples
3663-
--------
3664-
>>> np.random.seed(123)
3665-
>>> x = np.random.randint(9, size=(3, 3))
3666-
>>> x
3667-
array([[2, 2, 6],
3668-
[1, 3, 6],
3669-
[1, 0, 1]])
3670-
>>> x.itemset(4, 0)
3671-
>>> x.itemset((2, 2), 9)
3672-
>>> x
3673-
array([[2, 2, 6],
3674-
[1, 0, 6],
3675-
[1, 0, 9]])
3676-
3677-
"""))
3678-
3679-
36803633
add_newdoc('numpy.core.multiarray', 'ndarray', ('max',
36813634
"""
36823635
a.max(axis=None, out=None, keepdims=False, initial=<no value>, where=True)
@@ -3722,46 +3675,6 @@
37223675
"""))
37233676

37243677

3725-
add_newdoc('numpy.core.multiarray', 'ndarray', ('newbyteorder',
3726-
"""
3727-
arr.newbyteorder(new_order='S', /)
3728-
3729-
Return the array with the same data viewed with a different byte order.
3730-
3731-
Equivalent to::
3732-
3733-
arr.view(arr.dtype.newbytorder(new_order))
3734-
3735-
Changes are also made in all fields and sub-arrays of the array data
3736-
type.
3737-
3738-
3739-
3740-
Parameters
3741-
----------
3742-
new_order : string, optional
3743-
Byte order to force; a value from the byte order specifications
3744-
below. `new_order` codes can be any of:
3745-
3746-
* 'S' - swap dtype from current to opposite endian
3747-
* {'<', 'little'} - little endian
3748-
* {'>', 'big'} - big endian
3749-
* {'=', 'native'} - native order, equivalent to `sys.byteorder`
3750-
* {'|', 'I'} - ignore (no change to byte order)
3751-
3752-
The default value ('S') results in swapping the current
3753-
byte order.
3754-
3755-
3756-
Returns
3757-
-------
3758-
new_arr : array
3759-
New array object with the dtype reflecting given change to the
3760-
byte order.
3761-
3762-
"""))
3763-
3764-
37653678
add_newdoc('numpy.core.multiarray', 'ndarray', ('nonzero',
37663679
"""
37673680
a.nonzero()
@@ -3792,21 +3705,6 @@
37923705
"""))
37933706

37943707

3795-
add_newdoc('numpy.core.multiarray', 'ndarray', ('ptp',
3796-
"""
3797-
a.ptp(axis=None, out=None, keepdims=False)
3798-
3799-
Peak to peak (maximum - minimum) value along a given axis.
3800-
3801-
Refer to `numpy.ptp` for full documentation.
3802-
3803-
See Also
3804-
--------
3805-
numpy.ptp : equivalent function
3806-
3807-
"""))
3808-
3809-
38103708
add_newdoc('numpy.core.multiarray', 'ndarray', ('put',
38113709
"""
38123710
a.put(indices, values, mode='raise')
@@ -6724,9 +6622,6 @@ def refer_to_array_attribute(attr, method=True):
67246622
add_newdoc('numpy.core.numerictypes', 'generic',
67256623
refer_to_array_attribute('item'))
67266624

6727-
add_newdoc('numpy.core.numerictypes', 'generic',
6728-
refer_to_array_attribute('itemset'))
6729-
67306625
add_newdoc('numpy.core.numerictypes', 'generic',
67316626
refer_to_array_attribute('max'))
67326627

@@ -6736,46 +6631,12 @@ def refer_to_array_attribute(attr, method=True):
67366631
add_newdoc('numpy.core.numerictypes', 'generic',
67376632
refer_to_array_attribute('min'))
67386633

6739-
add_newdoc('numpy.core.numerictypes', 'generic', ('newbyteorder',
6740-
"""
6741-
newbyteorder(new_order='S', /)
6742-
6743-
Return a new `dtype` with a different byte order.
6744-
6745-
Changes are also made in all fields and sub-arrays of the data type.
6746-
6747-
The `new_order` code can be any from the following:
6748-
6749-
* 'S' - swap dtype from current to opposite endian
6750-
* {'<', 'little'} - little endian
6751-
* {'>', 'big'} - big endian
6752-
* {'=', 'native'} - native order
6753-
* {'|', 'I'} - ignore (no change to byte order)
6754-
6755-
Parameters
6756-
----------
6757-
new_order : str, optional
6758-
Byte order to force; a value from the byte order specifications
6759-
above. The default value ('S') results in swapping the current
6760-
byte order.
6761-
6762-
6763-
Returns
6764-
-------
6765-
new_dtype : dtype
6766-
New `dtype` object with the given change to the byte order.
6767-
6768-
"""))
6769-
67706634
add_newdoc('numpy.core.numerictypes', 'generic',
67716635
refer_to_array_attribute('nonzero'))
67726636

67736637
add_newdoc('numpy.core.numerictypes', 'generic',
67746638
refer_to_array_attribute('prod'))
67756639

6776-
add_newdoc('numpy.core.numerictypes', 'generic',
6777-
refer_to_array_attribute('ptp'))
6778-
67796640
add_newdoc('numpy.core.numerictypes', 'generic',
67806641
refer_to_array_attribute('put'))
67816642

numpy/core/fromnumeric.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,13 +2671,6 @@ def ptp(a, axis=None, out=None, keepdims=np._NoValue):
26712671
kwargs = {}
26722672
if keepdims is not np._NoValue:
26732673
kwargs['keepdims'] = keepdims
2674-
if type(a) is not mu.ndarray:
2675-
try:
2676-
ptp = a.ptp
2677-
except AttributeError:
2678-
pass
2679-
else:
2680-
return ptp(axis=axis, out=out, **kwargs)
26812674
return _methods._ptp(a, axis=axis, out=out, **kwargs)
26822675

26832676

numpy/core/getlimits.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ def _get_machar(ftype):
333333
# Detect known / suspected types
334334
# ftype(-1.0) / ftype(10.0) is better than ftype('-0.1') because stold
335335
# may be deficient
336-
key = (ftype(-1.0) / ftype(10.)).newbyteorder('<').tobytes()
336+
key = (ftype(-1.0) / ftype(10.))
337+
key = key.view(key.dtype.newbyteorder("<")).tobytes()
337338
ma_like = None
338339
if ftype == ntypes.longdouble:
339340
# Could be 80 bit == 10 byte extended precision, where last bytes can

numpy/core/records.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ def fromfile(fd, dtype=None, shape=None, offset=0, formats=None,
881881
>>> a[5] = (0.5,10,'abcde')
882882
>>>
883883
>>> fd=TemporaryFile()
884-
>>> a = a.newbyteorder('<')
884+
>>> a = a.view(a.dtype.newbyteorder('<'))
885885
>>> a.tofile(fd)
886886
>>>
887887
>>> _ = fd.seek(0)

0 commit comments

Comments
 (0)