summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorSebastian Berg <sebastianb@nvidia.com>2023-03-16 23:39:08 +0100
committerGitHub <noreply@github.com>2023-03-16 23:39:08 +0100
commite42c9503a14d66adfd41356ef5640c6975c45218 (patch)
tree5595654476cd1e50e15494022d287cbfabd7ab99 /numpy
parent6c31fef645b244d0a22fcc5597b42b6c650ca75e (diff)
parent21d9807fe52e42f72068f9f7629a8503a9fdb92f (diff)
downloadnumpy-e42c9503a14d66adfd41356ef5640c6975c45218.tar.gz
Merge pull request #23403 from mattip/clip-cleanup
DEP: remove deprecated casting in np.clip
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/_methods.py71
-rw-r--r--numpy/core/tests/test_numeric.py61
2 files changed, 20 insertions, 112 deletions
diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py
index 040f02a9d..0fc070b34 100644
--- a/numpy/core/_methods.py
+++ b/numpy/core/_methods.py
@@ -87,79 +87,16 @@ def _count_reduce_items(arr, axis, keepdims=False, where=True):
keepdims)
return items
-# Numpy 1.17.0, 2019-02-24
-# Various clip behavior deprecations, marked with _clip_dep as a prefix.
-
-def _clip_dep_is_scalar_nan(a):
- # guarded to protect circular imports
- from numpy.core.fromnumeric import ndim
- if ndim(a) != 0:
- return False
- try:
- return um.isnan(a)
- except TypeError:
- return False
-
-def _clip_dep_is_byte_swapped(a):
- if isinstance(a, mu.ndarray):
- return not a.dtype.isnative
- return False
-
-def _clip_dep_invoke_with_casting(ufunc, *args, out=None, casting=None, **kwargs):
- # normal path
- if casting is not None:
- return ufunc(*args, out=out, casting=casting, **kwargs)
-
- # try to deal with broken casting rules
- try:
- return ufunc(*args, out=out, **kwargs)
- except _exceptions._UFuncOutputCastingError as e:
- # Numpy 1.17.0, 2019-02-24
- warnings.warn(
- "Converting the output of clip from {!r} to {!r} is deprecated. "
- "Pass `casting=\"unsafe\"` explicitly to silence this warning, or "
- "correct the type of the variables.".format(e.from_, e.to),
- DeprecationWarning,
- stacklevel=2
- )
- return ufunc(*args, out=out, casting="unsafe", **kwargs)
-
-def _clip(a, min=None, max=None, out=None, *, casting=None, **kwargs):
+def _clip(a, min=None, max=None, out=None, **kwargs):
if min is None and max is None:
raise ValueError("One of max or min must be given")
- # Numpy 1.17.0, 2019-02-24
- # This deprecation probably incurs a substantial slowdown for small arrays,
- # it will be good to get rid of it.
- if not _clip_dep_is_byte_swapped(a) and not _clip_dep_is_byte_swapped(out):
- using_deprecated_nan = False
- if _clip_dep_is_scalar_nan(min):
- min = -float('inf')
- using_deprecated_nan = True
- if _clip_dep_is_scalar_nan(max):
- max = float('inf')
- using_deprecated_nan = True
- if using_deprecated_nan:
- warnings.warn(
- "Passing `np.nan` to mean no clipping in np.clip has always "
- "been unreliable, and is now deprecated. "
- "In future, this will always return nan, like it already does "
- "when min or max are arrays that contain nan. "
- "To skip a bound, pass either None or an np.inf of an "
- "appropriate sign.",
- DeprecationWarning,
- stacklevel=2
- )
-
if min is None:
- return _clip_dep_invoke_with_casting(
- um.minimum, a, max, out=out, casting=casting, **kwargs)
+ return um.minimum(a, max, out=out, **kwargs)
elif max is None:
- return _clip_dep_invoke_with_casting(
- um.maximum, a, min, out=out, casting=casting, **kwargs)
+ return um.maximum(a, min, out=out, **kwargs)
else:
- return _clip_dep_invoke_with_casting(
- um.clip, a, min, max, out=out, casting=casting, **kwargs)
+ return um.clip(a, min, max, out=out, **kwargs)
def _mean(a, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
arr = asanyarray(a)
diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py
index f81f563cd..832a47c92 100644
--- a/numpy/core/tests/test_numeric.py
+++ b/numpy/core/tests/test_numeric.py
@@ -1824,20 +1824,11 @@ class TestClip:
self.nr = 5
self.nc = 3
- def fastclip(self, a, m, M, out=None, casting=None):
- if out is None:
- if casting is None:
- return a.clip(m, M)
- else:
- return a.clip(m, M, casting=casting)
- else:
- if casting is None:
- return a.clip(m, M, out)
- else:
- return a.clip(m, M, out, casting=casting)
+ def fastclip(self, a, m, M, out=None, **kwargs):
+ return a.clip(m, M, out=out, **kwargs)
def clip(self, a, m, M, out=None):
- # use slow-clip
+ # use a.choose to verify fastclip result
selector = np.less(a, m) + 2*np.greater(a, M)
return selector.choose((a, m, M), out=out)
@@ -1993,14 +1984,13 @@ class TestClip:
ac = np.zeros(a.shape, dtype=np.int32)
act = ac.copy()
if casting is None:
- with assert_warns(DeprecationWarning):
- # NumPy 1.17.0, 2018-02-24 - casting is unsafe
+ with pytest.raises(TypeError):
self.fastclip(a, m, M, ac, casting=casting)
else:
# explicitly passing "unsafe" will silence warning
self.fastclip(a, m, M, ac, casting=casting)
- self.clip(a, m, M, act)
- assert_array_strict_equal(ac, act)
+ self.clip(a, m, M, act)
+ assert_array_strict_equal(ac, act)
def test_simple_int64_out(self):
# Test native int32 input with int32 scalar min/max and int64 out.
@@ -2020,9 +2010,7 @@ class TestClip:
M = np.float64(1)
ac = np.zeros(a.shape, dtype=np.int32)
act = ac.copy()
- with assert_warns(DeprecationWarning):
- # NumPy 1.17.0, 2018-02-24 - casting is unsafe
- self.fastclip(a, m, M, ac)
+ self.fastclip(a, m, M, out=ac, casting="unsafe")
self.clip(a, m, M, act)
assert_array_strict_equal(ac, act)
@@ -2033,9 +2021,7 @@ class TestClip:
M = 2.0
ac = np.zeros(a.shape, dtype=np.int32)
act = ac.copy()
- with assert_warns(DeprecationWarning):
- # NumPy 1.17.0, 2018-02-24 - casting is unsafe
- self.fastclip(a, m, M, ac)
+ self.fastclip(a, m, M, out=ac, casting="unsafe")
self.clip(a, m, M, act)
assert_array_strict_equal(ac, act)
@@ -2211,9 +2197,7 @@ class TestClip:
M = np.float64(2)
ac = np.zeros(a.shape, dtype=np.int32)
act = ac.copy()
- with assert_warns(DeprecationWarning):
- # NumPy 1.17.0, 2018-02-24 - casting is unsafe
- self.fastclip(a, m, M, ac)
+ self.fastclip(a, m, M, out=ac, casting="unsafe")
self.clip(a, m, M, act)
assert_array_strict_equal(ac, act)
@@ -2235,9 +2219,7 @@ class TestClip:
M = np.float64(1)
ac = np.zeros(a.shape, dtype=np.int32)
act = ac.copy()
- with assert_warns(DeprecationWarning):
- # NumPy 1.17.0, 2018-02-24 - casting is unsafe
- self.fastclip(a, m, M, ac)
+ self.fastclip(a, m, M, out=ac, casting="unsafe")
self.clip(a, m, M, act)
assert_array_strict_equal(ac, act)
@@ -2248,9 +2230,7 @@ class TestClip:
M = 2.0
ac = np.zeros(a.shape, dtype=np.int32)
act = ac.copy()
- with assert_warns(DeprecationWarning):
- # NumPy 1.17.0, 2018-02-24 - casting is unsafe
- self.fastclip(a, m, M, ac)
+ self.fastclip(a, m, M, out=ac, casting="unsafe")
self.clip(a, m, M, act)
assert_array_strict_equal(ac, act)
@@ -2303,16 +2283,11 @@ class TestClip:
def test_clip_nan(self):
d = np.arange(7.)
- with assert_warns(DeprecationWarning):
- assert_equal(d.clip(min=np.nan), d)
- with assert_warns(DeprecationWarning):
- assert_equal(d.clip(max=np.nan), d)
- with assert_warns(DeprecationWarning):
- assert_equal(d.clip(min=np.nan, max=np.nan), d)
- with assert_warns(DeprecationWarning):
- assert_equal(d.clip(min=-2, max=np.nan), d)
- with assert_warns(DeprecationWarning):
- assert_equal(d.clip(min=np.nan, max=10), d)
+ assert_equal(d.clip(min=np.nan), np.nan)
+ assert_equal(d.clip(max=np.nan), np.nan)
+ assert_equal(d.clip(min=np.nan, max=np.nan), np.nan)
+ assert_equal(d.clip(min=-2, max=np.nan), np.nan)
+ assert_equal(d.clip(min=np.nan, max=10), np.nan)
def test_object_clip(self):
a = np.arange(10, dtype=object)
@@ -2364,16 +2339,12 @@ class TestClip:
actual = np.clip(arr, amin, amax)
assert_equal(actual, exp)
- @pytest.mark.xfail(reason="no scalar nan propagation yet",
- raises=AssertionError,
- strict=True)
@pytest.mark.parametrize("arr, amin, amax", [
# problematic scalar nan case from hypothesis
(np.zeros(10, dtype=np.int64),
np.array(np.nan),
np.zeros(10, dtype=np.int32)),
])
- @pytest.mark.filterwarnings("ignore::DeprecationWarning")
def test_clip_scalar_nan_propagation(self, arr, amin, amax):
# enforcement of scalar nan propagation for comparisons
# called through clip()