summaryrefslogtreecommitdiff
path: root/numpy/lib/tests
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib/tests')
-rw-r--r--numpy/lib/tests/test_nanfunctions.py50
-rw-r--r--numpy/lib/tests/test_polynomial.py31
2 files changed, 60 insertions, 21 deletions
diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py
index e0f723a3c..1f1f5601b 100644
--- a/numpy/lib/tests/test_nanfunctions.py
+++ b/numpy/lib/tests/test_nanfunctions.py
@@ -588,6 +588,15 @@ class TestNanFunctions_MeanVarStd(SharedNanFunctionsTestsMixin):
assert_(len(w) == 0)
+_TIME_UNITS = (
+ "Y", "M", "W", "D", "h", "m", "s", "ms", "us", "ns", "ps", "fs", "as"
+)
+
+# All `inexact` + `timdelta64` type codes
+_TYPE_CODES = list(np.typecodes["AllFloat"])
+_TYPE_CODES += [f"m8[{unit}]" for unit in _TIME_UNITS]
+
+
class TestNanFunctions_Median:
def test_mutation(self):
@@ -662,23 +671,32 @@ class TestNanFunctions_Median:
res = np.nanmedian(_ndat, axis=1)
assert_almost_equal(res, tgt)
- def test_allnans(self):
- mat = np.array([np.nan]*9).reshape(3, 3)
- for axis in [None, 0, 1]:
- with suppress_warnings() as sup:
- sup.record(RuntimeWarning)
+ @pytest.mark.parametrize("axis", [None, 0, 1])
+ @pytest.mark.parametrize("dtype", _TYPE_CODES)
+ def test_allnans(self, dtype, axis):
+ mat = np.full((3, 3), np.nan).astype(dtype)
+ with suppress_warnings() as sup:
+ sup.record(RuntimeWarning)
- assert_(np.isnan(np.nanmedian(mat, axis=axis)).all())
- if axis is None:
- assert_(len(sup.log) == 1)
- else:
- assert_(len(sup.log) == 3)
- # Check scalar
- assert_(np.isnan(np.nanmedian(np.nan)))
- if axis is None:
- assert_(len(sup.log) == 2)
- else:
- assert_(len(sup.log) == 4)
+ output = np.nanmedian(mat, axis=axis)
+ assert output.dtype == mat.dtype
+ assert np.isnan(output).all()
+
+ if axis is None:
+ assert_(len(sup.log) == 1)
+ else:
+ assert_(len(sup.log) == 3)
+
+ # Check scalar
+ scalar = np.array(np.nan).astype(dtype)[()]
+ output_scalar = np.nanmedian(scalar)
+ assert output_scalar.dtype == scalar.dtype
+ assert np.isnan(output_scalar)
+
+ if axis is None:
+ assert_(len(sup.log) == 2)
+ else:
+ assert_(len(sup.log) == 4)
def test_empty(self):
mat = np.zeros((0, 3))
diff --git a/numpy/lib/tests/test_polynomial.py b/numpy/lib/tests/test_polynomial.py
index 6c3e4fa02..3734344d2 100644
--- a/numpy/lib/tests/test_polynomial.py
+++ b/numpy/lib/tests/test_polynomial.py
@@ -4,6 +4,12 @@ from numpy.testing import (
assert_array_almost_equal, assert_raises, assert_allclose
)
+import pytest
+
+# `poly1d` has some support for `bool_` and `timedelta64`,
+# but it is limited and they are therefore excluded here
+TYPE_CODES = np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "O"
+
class TestPolynomial:
def test_poly1d_str_and_repr(self):
@@ -57,11 +63,26 @@ class TestPolynomial:
assert_equal(np.polydiv(np.poly1d([1, 0, -1]), np.poly1d([1, 1])),
(np.poly1d([1., -1.]), np.poly1d([0.])))
- def test_poly1d_misc(self):
- p = np.poly1d([1., 2, 3])
- assert_equal(np.asarray(p), np.array([1., 2., 3.]))
+ @pytest.mark.parametrize("type_code", TYPE_CODES)
+ def test_poly1d_misc(self, type_code: str) -> None:
+ dtype = np.dtype(type_code)
+ ar = np.array([1, 2, 3], dtype=dtype)
+ p = np.poly1d(ar)
+
+ # `__eq__`
+ assert_equal(np.asarray(p), ar)
+ assert_equal(np.asarray(p).dtype, dtype)
assert_equal(len(p), 2)
- assert_equal((p[0], p[1], p[2], p[3]), (3.0, 2.0, 1.0, 0))
+
+ # `__getitem__`
+ comparison_dct = {-1: 0, 0: 3, 1: 2, 2: 1, 3: 0}
+ for index, ref in comparison_dct.items():
+ scalar = p[index]
+ assert_equal(scalar, ref)
+ if dtype == np.object_:
+ assert isinstance(scalar, int)
+ else:
+ assert_equal(scalar.dtype, dtype)
def test_poly1d_variable_arg(self):
q = np.poly1d([1., 2, 3], variable='y')
@@ -257,7 +278,7 @@ class TestPolynomial:
assert_equal(q.coeffs.dtype, np.complex128)
assert_equal(r.coeffs.dtype, np.complex128)
assert_equal(q*a + r, b)
-
+
c = [1, 2, 3]
d = np.poly1d([1, 2, 3])
s, t = np.polydiv(c, d)