summaryrefslogtreecommitdiff
path: root/numpy/lib/tests/test_function_base.py
diff options
context:
space:
mode:
authorKevin Sheppard <bashtage@users.noreply.github.com>2021-04-22 14:35:43 +0100
committerGitHub <noreply@github.com>2021-04-22 08:35:43 -0500
commit341316d5158477b06f877db60049b0995ab78128 (patch)
tree3cabe19b30eaed52551ba58a7dfa4945f688fda6 /numpy/lib/tests/test_function_base.py
parentf048051926bdc1f145b4e6a9516b0ee55b5d1e8e (diff)
downloadnumpy-341316d5158477b06f877db60049b0995ab78128.tar.gz
BUG: Prevent nan being used in percentile (gh-18831)
Reject NaN as a percentile/quantile value. Previously NaNs could pass the range check `0 <= q <= 1`. closes #18830
Diffstat (limited to 'numpy/lib/tests/test_function_base.py')
-rw-r--r--numpy/lib/tests/test_function_base.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index 761ea83a3..0b66ccf8c 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -2750,6 +2750,10 @@ class TestPercentile:
assert_equal(p, Fraction(7, 4))
assert_equal(type(p), Fraction)
+ p = np.percentile(x, [Fraction(50)])
+ assert_equal(p, np.array([Fraction(7, 4)]))
+ assert_equal(type(p), np.ndarray)
+
def test_api(self):
d = np.ones(5)
np.percentile(d, 5, None, None, False)
@@ -3144,6 +3148,16 @@ class TestPercentile:
assert_equal(np.percentile(
a, [0.3, 0.6], (0, 2), interpolation='nearest'), b)
+ def test_nan_q(self):
+ # GH18830
+ with pytest.raises(ValueError, match="Percentiles must be in"):
+ np.percentile([1, 2, 3, 4.0], np.nan)
+ with pytest.raises(ValueError, match="Percentiles must be in"):
+ np.percentile([1, 2, 3, 4.0], [np.nan])
+ q = np.linspace(1.0, 99.0, 16)
+ q[0] = np.nan
+ with pytest.raises(ValueError, match="Percentiles must be in"):
+ np.percentile([1, 2, 3, 4.0], q)
class TestQuantile:
# most of this is already tested by TestPercentile
@@ -3180,6 +3194,14 @@ class TestQuantile:
assert_equal(q, Fraction(7, 4))
assert_equal(type(q), Fraction)
+ q = np.quantile(x, [Fraction(1, 2)])
+ assert_equal(q, np.array([Fraction(7, 4)]))
+ assert_equal(type(q), np.ndarray)
+
+ q = np.quantile(x, [[Fraction(1, 2)]])
+ assert_equal(q, np.array([[Fraction(7, 4)]]))
+ assert_equal(type(q), np.ndarray)
+
# repeat with integral input but fractional quantile
x = np.arange(8)
assert_equal(np.quantile(x, Fraction(1, 2)), Fraction(7, 2))