diff options
| author | Allan Haldane <allan.haldane@gmail.com> | 2016-10-11 14:47:11 -0400 |
|---|---|---|
| committer | Allan Haldane <allan.haldane@gmail.com> | 2016-10-13 18:50:32 -0400 |
| commit | d8d7c2528b99094358a57e53bb7a67bcacb679ae (patch) | |
| tree | a61d2e8a04de066a2cba27046f61c5080562e98f /numpy | |
| parent | a6574b2f080e4050e4b091fd3fa71be1f377f895 (diff) | |
| download | numpy-d8d7c2528b99094358a57e53bb7a67bcacb679ae.tar.gz | |
BUG: np.ma.mean and var should return scalar if no mask
Fixes #5769
Diffstat (limited to 'numpy')
| -rw-r--r-- | numpy/ma/core.py | 12 | ||||
| -rw-r--r-- | numpy/ma/tests/test_core.py | 16 |
2 files changed, 25 insertions, 3 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index f83e2adcc..cef933d01 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -5057,7 +5057,7 @@ class MaskedArray(ndarray): if self._mask is nomask: result = super(MaskedArray, self).mean(axis=axis, - dtype=dtype, **kwargs) + dtype=dtype, **kwargs)[()] else: dsum = self.sum(axis=axis, dtype=dtype, **kwargs) cnt = self.count(axis=axis, **kwargs) @@ -5134,8 +5134,14 @@ class MaskedArray(ndarray): # Easy case: nomask, business as usual if self._mask is nomask: - return self._data.var(axis=axis, dtype=dtype, out=out, - ddof=ddof, **kwargs) + ret = super(MaskedArray, self).var(axis=axis, dtype=dtype, out=out, + ddof=ddof, **kwargs)[()] + if out is not None: + if isinstance(out, MaskedArray): + out.__setmask__(nomask) + return out + return ret + # Some data are masked, yay! cnt = self.count(axis=axis, **kwargs) - ddof danom = self - self.mean(axis, dtype, keepdims=True) diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 338a6d0dc..5ca0e0b28 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -3340,6 +3340,22 @@ class TestMaskedArrayMathMethods(TestCase): assert_almost_equal(z.filled(0), [[1, 0], [15, 16]]) assert_almost_equal(z.mask, [[0, 1], [0, 0]]) + def test_varmean_nomask(self): + # gh-5769 + foo = array([1,2,3,4], dtype='f8') + bar = array([1,2,3,4], dtype='f8') + assert_equal(type(foo.mean()), np.float64) + assert_equal(type(foo.var()), np.float64) + assert((foo.mean() == bar.mean()) is np.bool_(True)) + + # check array type is preserved and out works + foo = array(np.arange(16).reshape((4,4)), dtype='f8') + bar = empty(4, dtype='f4') + assert_equal(type(foo.mean(axis=1)), MaskedArray) + assert_equal(type(foo.var(axis=1)), MaskedArray) + assert_(foo.mean(axis=1, out=bar) is bar) + assert_(foo.var(axis=1, out=bar) is bar) + def test_varstd(self): # Tests var & std on MaskedArrays. (x, X, XX, m, mx, mX, mXX, m2x, m2X, m2XX) = self.d |
