diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/__init__.py | 2 | ||||
-rw-r--r-- | numpy/__init__.pyi | 6 | ||||
-rw-r--r-- | numpy/core/__init__.py | 1 | ||||
-rw-r--r-- | numpy/core/fromnumeric.py | 160 | ||||
-rw-r--r-- | numpy/core/numeric.py | 5 | ||||
-rw-r--r-- | numpy/core/src/multiarray/experimental_public_dtype_api.c | 8 | ||||
-rw-r--r-- | numpy/core/tests/test_deprecations.py | 6 | ||||
-rw-r--r-- | numpy/lib/function_base.py | 4 | ||||
-rw-r--r-- | numpy/ma/tests/test_extras.py | 4 |
9 files changed, 134 insertions, 62 deletions
diff --git a/numpy/__init__.py b/numpy/__init__.py index 22c005518..e41f7eae6 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -221,7 +221,7 @@ else: del _msg, _type_info - from .core import round, abs, max, min + from .core import abs # now that numpy modules are imported, can initialize limits core.getlimits._register_known_types() diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index f144895be..a42e6c0e4 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -254,6 +254,8 @@ from numpy.core.fromnumeric import ( any as any, cumsum as cumsum, ptp as ptp, + max as max, + min as min, amax as amax, amin as amin, prod as prod, @@ -261,6 +263,7 @@ from numpy.core.fromnumeric import ( ndim as ndim, size as size, around as around, + round as round, mean as mean, std as std, var as var, @@ -667,10 +670,7 @@ test: PytestTester # Placeholders for classes # Some of these are aliases; others are wrappers with an identical signature -round = around round_ = around -max = amax -min = amin product = prod cumproduct = cumprod sometrue = any diff --git a/numpy/core/__init__.py b/numpy/core/__init__.py index 1079c41df..142c24ee0 100644 --- a/numpy/core/__init__.py +++ b/numpy/core/__init__.py @@ -92,7 +92,6 @@ from . import einsumfunc from .einsumfunc import * del nt -from .fromnumeric import amax as max, amin as min, round_ as round from .numeric import absolute as abs # do this after everything else, to minimize the chance of this misleadingly diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index e7366898e..ed8b68ecd 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -6,6 +6,7 @@ import types import warnings import numpy as np +from .._utils import set_module from . import multiarray as mu from . import overrides from . import umath as um @@ -20,8 +21,9 @@ __all__ = [ 'all', 'alltrue', 'amax', 'amin', 'any', 'argmax', 'argmin', 'argpartition', 'argsort', 'around', 'choose', 'clip', 'compress', 'cumprod', 'cumproduct', 'cumsum', 'diagonal', 'mean', + 'max', 'min', 'ndim', 'nonzero', 'partition', 'prod', 'product', 'ptp', 'put', - 'ravel', 'repeat', 'reshape', 'resize', 'round_', + 'ravel', 'repeat', 'reshape', 'resize', 'round', 'round_', 'searchsorted', 'shape', 'size', 'sometrue', 'sort', 'squeeze', 'std', 'sum', 'swapaxes', 'take', 'trace', 'transpose', 'var', ] @@ -2695,13 +2697,14 @@ def ptp(a, axis=None, out=None, keepdims=np._NoValue): return _methods._ptp(a, axis=axis, out=out, **kwargs) -def _amax_dispatcher(a, axis=None, out=None, keepdims=None, initial=None, - where=None): +def _max_dispatcher(a, axis=None, out=None, keepdims=None, initial=None, + where=None): return (a, out) -@array_function_dispatch(_amax_dispatcher) -def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, +@array_function_dispatch(_max_dispatcher) +@set_module('numpy') +def max(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, where=np._NoValue): """ Return the maximum of an array or maximum along an axis. @@ -2729,7 +2732,7 @@ def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, the result will broadcast correctly against the input array. If the default value is passed, then `keepdims` will not be - passed through to the `amax` method of sub-classes of + passed through to the ``max`` method of sub-classes of `ndarray`, however any non-default value will be. If the sub-class' method does not implement `keepdims` any exceptions will be raised. @@ -2748,7 +2751,7 @@ def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, Returns ------- - amax : ndarray or scalar + max : ndarray or scalar Maximum of `a`. If `axis` is None, the result is a scalar value. If `axis` is an int, the result is an array of dimension ``a.ndim - 1``. If `axis` is a tuple, the result is an array of @@ -2775,9 +2778,9 @@ def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, corresponding max value will be NaN as well. To ignore NaN values (MATLAB behavior), please use nanmax. - Don't use `amax` for element-wise comparison of 2 arrays; when + Don't use `~numpy.max` for element-wise comparison of 2 arrays; when ``a.shape[0]`` is 2, ``maximum(a[0], a[1])`` is faster than - ``amax(a, axis=0)``. + ``max(a, axis=0)``. Examples -------- @@ -2785,19 +2788,19 @@ def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, >>> a array([[0, 1], [2, 3]]) - >>> np.amax(a) # Maximum of the flattened array + >>> np.max(a) # Maximum of the flattened array 3 - >>> np.amax(a, axis=0) # Maxima along the first axis + >>> np.max(a, axis=0) # Maxima along the first axis array([2, 3]) - >>> np.amax(a, axis=1) # Maxima along the second axis + >>> np.max(a, axis=1) # Maxima along the second axis array([1, 3]) - >>> np.amax(a, where=[False, True], initial=-1, axis=0) + >>> np.max(a, where=[False, True], initial=-1, axis=0) array([-1, 3]) >>> b = np.arange(5, dtype=float) >>> b[2] = np.NaN - >>> np.amax(b) + >>> np.max(b) nan - >>> np.amax(b, where=~np.isnan(b), initial=-1) + >>> np.max(b, where=~np.isnan(b), initial=-1) 4.0 >>> np.nanmax(b) 4.0 @@ -2805,14 +2808,14 @@ def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, You can use an initial value to compute the maximum of an empty slice, or to initialize it to a different value: - >>> np.amax([[-50], [10]], axis=-1, initial=0) + >>> np.max([[-50], [10]], axis=-1, initial=0) array([ 0, 10]) Notice that the initial value is used as one of the elements for which the maximum is determined, unlike for the default argument Python's max function, which is only used for empty iterables. - >>> np.amax([5], initial=6) + >>> np.max([5], initial=6) 6 >>> max([5], default=6) 5 @@ -2821,14 +2824,31 @@ def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, keepdims=keepdims, initial=initial, where=where) -def _amin_dispatcher(a, axis=None, out=None, keepdims=None, initial=None, - where=None): +@array_function_dispatch(_max_dispatcher) +def amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, + where=np._NoValue): + """ + Return the maximum of an array or maximum along an axis. + + `amax` is an alias of `~numpy.max`. + + See Also + -------- + max : alias of this function + ndarray.max : equivalent method + """ + return _wrapreduction(a, np.maximum, 'max', axis, None, out, + keepdims=keepdims, initial=initial, where=where) + + +def _min_dispatcher(a, axis=None, out=None, keepdims=None, initial=None, + where=None): return (a, out) -@array_function_dispatch(_amin_dispatcher) -def amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, - where=np._NoValue): +@array_function_dispatch(_min_dispatcher) +def min(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, + where=np._NoValue): """ Return the minimum of an array or minimum along an axis. @@ -2855,7 +2875,7 @@ def amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, the result will broadcast correctly against the input array. If the default value is passed, then `keepdims` will not be - passed through to the `amin` method of sub-classes of + passed through to the ``min`` method of sub-classes of `ndarray`, however any non-default value will be. If the sub-class' method does not implement `keepdims` any exceptions will be raised. @@ -2874,7 +2894,7 @@ def amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, Returns ------- - amin : ndarray or scalar + min : ndarray or scalar Minimum of `a`. If `axis` is None, the result is a scalar value. If `axis` is an int, the result is an array of dimension ``a.ndim - 1``. If `axis` is a tuple, the result is an array of @@ -2901,9 +2921,9 @@ def amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, corresponding min value will be NaN as well. To ignore NaN values (MATLAB behavior), please use nanmin. - Don't use `amin` for element-wise comparison of 2 arrays; when + Don't use `~numpy.min` for element-wise comparison of 2 arrays; when ``a.shape[0]`` is 2, ``minimum(a[0], a[1])`` is faster than - ``amin(a, axis=0)``. + ``min(a, axis=0)``. Examples -------- @@ -2911,25 +2931,25 @@ def amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, >>> a array([[0, 1], [2, 3]]) - >>> np.amin(a) # Minimum of the flattened array + >>> np.min(a) # Minimum of the flattened array 0 - >>> np.amin(a, axis=0) # Minima along the first axis + >>> np.min(a, axis=0) # Minima along the first axis array([0, 1]) - >>> np.amin(a, axis=1) # Minima along the second axis + >>> np.min(a, axis=1) # Minima along the second axis array([0, 2]) - >>> np.amin(a, where=[False, True], initial=10, axis=0) + >>> np.min(a, where=[False, True], initial=10, axis=0) array([10, 1]) >>> b = np.arange(5, dtype=float) >>> b[2] = np.NaN - >>> np.amin(b) + >>> np.min(b) nan - >>> np.amin(b, where=~np.isnan(b), initial=10) + >>> np.min(b, where=~np.isnan(b), initial=10) 0.0 >>> np.nanmin(b) 0.0 - >>> np.amin([[-50], [10]], axis=-1, initial=0) + >>> np.min([[-50], [10]], axis=-1, initial=0) array([-50, 0]) Notice that the initial value is used as one of the elements for which the @@ -2938,7 +2958,7 @@ def amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, Notice that this isn't the same as Python's ``default`` argument. - >>> np.amin([6], initial=5) + >>> np.min([6], initial=5) 5 >>> min([6], default=5) 6 @@ -2947,6 +2967,23 @@ def amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, keepdims=keepdims, initial=initial, where=where) +@array_function_dispatch(_min_dispatcher) +def amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, + where=np._NoValue): + """ + Return the minimum of an array or minimum along an axis. + + `amin` is an alias of `~numpy.min`. + + See Also + -------- + min : alias of this function + ndarray.min : equivalent method + """ + return _wrapreduction(a, np.minimum, 'min', axis, None, out, + keepdims=keepdims, initial=initial, where=where) + + def _prod_dispatcher(a, axis=None, dtype=None, out=None, keepdims=None, initial=None, where=None): return (a, out) @@ -3238,12 +3275,12 @@ def size(a, axis=None): return asarray(a).shape[axis] -def _around_dispatcher(a, decimals=None, out=None): +def _round_dispatcher(a, decimals=None, out=None): return (a, out) -@array_function_dispatch(_around_dispatcher) -def around(a, decimals=0, out=None): +@array_function_dispatch(_round_dispatcher) +def round(a, decimals=0, out=None): """ Evenly round to the given number of decimals. @@ -3274,18 +3311,17 @@ def around(a, decimals=0, out=None): See Also -------- ndarray.round : equivalent method + around : an alias for this function ceil, fix, floor, rint, trunc Notes ----- - `~numpy.round` is often used as an alias for `~numpy.around`. - For values exactly halfway between rounded decimal values, NumPy rounds to the nearest even value. Thus 1.5 and 2.5 round to 2.0, -0.5 and 0.5 round to 0.0, etc. - ``np.around`` uses a fast but sometimes inexact algorithm to round + ``np.round`` uses a fast but sometimes inexact algorithm to round floating-point datatypes. For positive `decimals` it is equivalent to ``np.true_divide(np.rint(a * 10**decimals), 10**decimals)``, which has error due to the inexact representation of decimal fractions in the IEEE @@ -3322,21 +3358,38 @@ def around(a, decimals=0, out=None): Examples -------- - >>> np.around([0.37, 1.64]) + >>> np.round([0.37, 1.64]) array([0., 2.]) - >>> np.around([0.37, 1.64], decimals=1) + >>> np.round([0.37, 1.64], decimals=1) array([0.4, 1.6]) - >>> np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value + >>> np.round([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value array([0., 2., 2., 4., 4.]) - >>> np.around([1,2,3,11], decimals=1) # ndarray of ints is returned + >>> np.round([1,2,3,11], decimals=1) # ndarray of ints is returned array([ 1, 2, 3, 11]) - >>> np.around([1,2,3,11], decimals=-1) + >>> np.round([1,2,3,11], decimals=-1) array([ 0, 0, 0, 10]) """ return _wrapfunc(a, 'round', decimals=decimals, out=out) +@array_function_dispatch(_round_dispatcher) +def around(a, decimals=0, out=None): + """ + Round an array to the given number of decimals. + + `around` is an alias of `~numpy.round`. + + See Also + -------- + ndarray.round : equivalent method + round : alias for this function + ceil, fix, floor, rint, trunc + + """ + return _wrapfunc(a, 'round', decimals=decimals, out=out) + + def _mean_dispatcher(a, axis=None, dtype=None, out=None, keepdims=None, *, where=None): return (a, where, out) @@ -3748,18 +3801,29 @@ def var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue, *, **kwargs) -# Aliases of other functions. These have their own definitions only so that -# they can have unique docstrings. +# Aliases of other functions. Provided unique docstrings +# are reference purposes only. Wherever possible, +# avoid using them. -@array_function_dispatch(_around_dispatcher) +@array_function_dispatch(_round_dispatcher) def round_(a, decimals=0, out=None): """ Round an array to the given number of decimals. + `~numpy.round_` is a disrecommended backwards-compatibility + alias of `~numpy.around` and `~numpy.round`. + + .. deprecated:: 1.25.0 + ``round_`` is deprecated as of NumPy 1.25.0, and will be + removed in NumPy 2.0. Please use `round` instead. + See Also -------- around : equivalent function; see for details. """ + warnings.warn("`round_` is deprecated as of NumPy 1.25.0, and will be " + "removed in NumPy 2.0. Please use `round` instead.", + DeprecationWarning, stacklevel=2) return around(a, decimals=decimals, out=out) diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index fbb284696..1687e2dbf 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -4,6 +4,7 @@ import operator import sys import warnings import numbers +import builtins import numpy as np from . import multiarray @@ -2023,7 +2024,7 @@ def binary_repr(num, width=None): binary = bin(num)[2:] binwidth = len(binary) outwidth = (binwidth if width is None - else max(binwidth, width)) + else builtins.max(binwidth, width)) warn_if_insufficient(width, binwidth) return binary.zfill(outwidth) @@ -2043,7 +2044,7 @@ def binary_repr(num, width=None): binary = bin(twocomp)[2:] binwidth = len(binary) - outwidth = max(binwidth, width) + outwidth = builtins.max(binwidth, width) warn_if_insufficient(width, binwidth) return '1' * (outwidth - binwidth) + binary diff --git a/numpy/core/src/multiarray/experimental_public_dtype_api.c b/numpy/core/src/multiarray/experimental_public_dtype_api.c index 70a6f1995..c0da0a7b7 100644 --- a/numpy/core/src/multiarray/experimental_public_dtype_api.c +++ b/numpy/core/src/multiarray/experimental_public_dtype_api.c @@ -138,10 +138,12 @@ PyArrayInitDTypeMeta_FromSpec( } /* Check and handle flags: */ - if (spec->flags & ~(NPY_DT_PARAMETRIC|NPY_DT_ABSTRACT)) { + int allowed_flags = NPY_DT_PARAMETRIC | NPY_DT_ABSTRACT | NPY_DT_NUMERIC; + if (spec->flags & ~(allowed_flags)) { PyErr_SetString(PyExc_RuntimeError, - "invalid DType flags specified, only parametric and abstract " - "are valid flags for user DTypes."); + "invalid DType flags specified, only NPY_DT_PARAMETRIC, " + "NPY_DT_ABSTRACT, and NPY_DT_NUMERIC are valid flags for " + "user DTypes."); return -1; } diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index ac4761b50..8ff52c885 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -901,3 +901,9 @@ class TestDeprecatedFinfo(_DeprecationTestCase): # Deprecated in NumPy 1.25, 2023-01-16 def test_deprecated_none(self): self.assert_deprecated(np.finfo, args=(None,)) + + +class TestRound_(_DeprecationTestCase): + # 2023-02-28, 1.25.0 + def test_round_(self): + self.assert_deprecated(lambda: np.round_(np.array([1.5, 2.5, 3.5]))) diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index f5af314b5..405790025 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -4937,7 +4937,7 @@ def _meshgrid_dispatcher(*xi, copy=None, sparse=None, indexing=None): @array_function_dispatch(_meshgrid_dispatcher) def meshgrid(*xi, copy=True, sparse=False, indexing='xy'): """ - Return coordinate matrices from coordinate vectors. + Return a list of coordinate matrices from coordinate vectors. Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given @@ -4978,7 +4978,7 @@ def meshgrid(*xi, copy=True, sparse=False, indexing='xy'): Returns ------- - X1, X2,..., XN : ndarray + X1, X2,..., XN : list of ndarrays For vectors `x1`, `x2`,..., `xn` with lengths ``Ni=len(xi)``, returns ``(N1, N2, N3,..., Nn)`` shaped arrays if indexing='ij' or ``(N2, N1, N3,..., Nn)`` shaped arrays if indexing='xy' diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py index 38603fb84..e59ba3656 100644 --- a/numpy/ma/tests/test_extras.py +++ b/numpy/ma/tests/test_extras.py @@ -387,8 +387,8 @@ class TestConcatenator: # Tests mr_ on 2D arrays. a_1 = np.random.rand(5, 5) a_2 = np.random.rand(5, 5) - m_1 = np.round_(np.random.rand(5, 5), 0) - m_2 = np.round_(np.random.rand(5, 5), 0) + m_1 = np.round(np.random.rand(5, 5), 0) + m_2 = np.round(np.random.rand(5, 5), 0) b_1 = masked_array(a_1, mask=m_1) b_2 = masked_array(a_2, mask=m_2) # append columns |