From 4f470b35308d93b44215c6adc22636e1176de6fd Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Thu, 9 Sep 2021 11:02:07 +0200 Subject: BUG: Fixed an issue wherein `var` would raise for 0d object arrays --- numpy/core/_methods.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py index e475b94df..a239e2c87 100644 --- a/numpy/core/_methods.py +++ b/numpy/core/_methods.py @@ -221,8 +221,10 @@ def _var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, if isinstance(arrmean, mu.ndarray): arrmean = um.true_divide(arrmean, div, out=arrmean, casting='unsafe', subok=False) - else: + elif hasattr(arrmean, "dtype"): arrmean = arrmean.dtype.type(arrmean / rcount) + else: + arrmean = arrmean / rcount # Compute sum of squared deviations from mean # Note that x may not be inexact and that we need it to be an array, -- cgit v1.2.1 From 1fe7024c0a3c0d5da985802b3a24a19e312b8673 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Thu, 9 Sep 2021 11:02:35 +0200 Subject: TST: Expand `TestNanFunctions_NumberTypes` with 0d arrays --- numpy/lib/tests/test_nanfunctions.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/numpy/lib/tests/test_nanfunctions.py b/numpy/lib/tests/test_nanfunctions.py index 7756f2d79..fd5e9599c 100644 --- a/numpy/lib/tests/test_nanfunctions.py +++ b/numpy/lib/tests/test_nanfunctions.py @@ -231,15 +231,20 @@ class TestNanFunctions_ArgminArgmax: assert_(res.shape == ()) +_TEST_ARRAYS = { + "0d": np.array(5), + "1d": np.array([127, 39, 93, 87, 46]) +} +for _v in _TEST_ARRAYS.values(): + _v.setflags(write=False) + + @pytest.mark.parametrize( "dtype", np.typecodes["AllInteger"] + np.typecodes["AllFloat"] + "O", ) +@pytest.mark.parametrize("mat", _TEST_ARRAYS.values(), ids=_TEST_ARRAYS.keys()) class TestNanFunctions_NumberTypes: - - mat = np.array([127, 39, 93, 87, 46]) - mat.setflags(write=False) - nanfuncs = { np.nanmin: np.min, np.nanmax: np.max, @@ -258,8 +263,8 @@ class TestNanFunctions_NumberTypes: @pytest.mark.parametrize("nanfunc,func", nanfuncs.items(), ids=nanfunc_ids) @np.errstate(over="ignore") - def test_nanfunc(self, dtype, nanfunc, func): - mat = self.mat.astype(dtype) + def test_nanfunc(self, mat, dtype, nanfunc, func): + mat = mat.astype(dtype) tgt = func(mat) out = nanfunc(mat) @@ -274,8 +279,8 @@ class TestNanFunctions_NumberTypes: [(np.nanquantile, np.quantile), (np.nanpercentile, np.percentile)], ids=["nanquantile", "nanpercentile"], ) - def test_nanfunc_q(self, dtype, nanfunc, func): - mat = self.mat.astype(dtype) + def test_nanfunc_q(self, mat, dtype, nanfunc, func): + mat = mat.astype(dtype) tgt = func(mat, q=1) out = nanfunc(mat, q=1) @@ -290,10 +295,10 @@ class TestNanFunctions_NumberTypes: [(np.nanvar, np.var), (np.nanstd, np.std)], ids=["nanvar", "nanstd"], ) - def test_nanfunc_ddof(self, dtype, nanfunc, func): - mat = self.mat.astype(dtype) - tgt = func(mat, ddof=1) - out = nanfunc(mat, ddof=1) + def test_nanfunc_ddof(self, mat, dtype, nanfunc, func): + mat = mat.astype(dtype) + tgt = func(mat, ddof=0.5) + out = nanfunc(mat, ddof=0.5) assert_almost_equal(out, tgt) if dtype == "O": -- cgit v1.2.1