From 9a23f16b67958205468f7f5c4e0966f393558d9a Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Tue, 23 Apr 2019 01:55:21 -0700 Subject: ENH: Add support for Fraction to percentile and quantile With true division available, using `.0` to convert integers to floats offers no value, and harms compatibility with precise rational types. --- numpy/lib/tests/test_function_base.py | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'numpy/lib/tests/test_function_base.py') diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index e2c24a123..6d32c365a 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -5,6 +5,7 @@ import warnings import sys import decimal import types +from fractions import Fraction import pytest import numpy as np @@ -2508,6 +2509,21 @@ class TestPercentile(object): assert_equal(np.percentile(x, 0), np.nan) assert_equal(np.percentile(x, 0, interpolation='nearest'), np.nan) + def test_fraction(self): + x = [Fraction(i, 2) for i in np.arange(8)] + + p = np.percentile(x, Fraction(0)) + assert_equal(p, Fraction(0)) + assert_equal(type(p), Fraction) + + p = np.percentile(x, Fraction(100)) + assert_equal(p, Fraction(7, 2)) + assert_equal(type(p), Fraction) + + p = np.percentile(x, Fraction(50)) + assert_equal(p, Fraction(7, 4)) + assert_equal(type(p), Fraction) + def test_api(self): d = np.ones(5) np.percentile(d, 5, None, None, False) @@ -2912,6 +2928,26 @@ class TestQuantile(object): assert_equal(np.quantile(x, 1), 3.5) assert_equal(np.quantile(x, 0.5), 1.75) + def test_fraction(self): + # fractional input, integral quantile + x = [Fraction(i, 2) for i in np.arange(8)] + + q = np.quantile(x, 0) + assert_equal(q, 0) + assert_equal(type(q), Fraction) + + q = np.quantile(x, 1) + assert_equal(q, Fraction(7, 2)) + assert_equal(type(q), Fraction) + + q = np.quantile(x, Fraction(1, 2)) + assert_equal(q, Fraction(7, 4)) + assert_equal(type(q), Fraction) + + # repeat with integral input but fractional quantile + x = np.arange(8) + assert_equal(np.quantile(x, Fraction(1, 2)), Fraction(7, 2)) + def test_no_p_overwrite(self): # this is worth retesting, because quantile does not make a copy p0 = np.array([0, 0.75, 0.25, 0.5, 1.0]) -- cgit v1.2.1 From d7de4ad70d6794b36e4789e4f6146a884113bd66 Mon Sep 17 00:00:00 2001 From: ayir Date: Wed, 10 Apr 2019 17:00:25 +0200 Subject: ENH: add clearer error message for diff(0-d) --- numpy/lib/tests/test_function_base.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'numpy/lib/tests/test_function_base.py') diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 1e04bfaec..8f1b56e3f 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -695,6 +695,9 @@ class TestDiff(object): assert_raises(np.AxisError, diff, x, axis=3) assert_raises(np.AxisError, diff, x, axis=-4) + x = np.array(1.11111111111, np.float64) + assert_raises(ValueError, diff, x) + def test_nd(self): x = 20 * rand(10, 20, 30) out1 = x[:, :, 1:] - x[:, :, :-1] -- cgit v1.2.1 From 599dbade766ad3fecc82afa308803a2e0082c149 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Thu, 28 Dec 2017 11:17:52 +0000 Subject: API: Make MaskedArray.mask return a view, rather than the underlying mask This prevents consumers from reshaping the mask in place, which breaks things As a result, `x.mask is x.mask` returns `False`, but this was already true of `x.data is x.data`. May also be related to gh-10270 --- numpy/lib/tests/test_function_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'numpy/lib/tests/test_function_base.py') diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 6d32c365a..0702c3856 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -945,7 +945,7 @@ class TestGradient(object): assert_equal(type(out), type(x)) # And make sure that the output and input don't have aliased mask # arrays - assert_(x.mask is not out.mask) + assert_(x._mask is not out._mask) # Also check that edge_order=2 doesn't alter the original mask x2 = np.ma.arange(5) x2[2] = np.ma.masked -- cgit v1.2.1