diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2016-10-14 13:15:37 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-14 13:15:37 -0400 |
commit | fa31422a411ef470fc38f9f11b4835911a626736 (patch) | |
tree | a45e89a46e9eece4c30161ff790df46bd3423100 | |
parent | ee41e9dfd2a0a6241e0d9e95fb355bfc92383fea (diff) | |
parent | d8d7c2528b99094358a57e53bb7a67bcacb679ae (diff) | |
download | numpy-fa31422a411ef470fc38f9f11b4835911a626736.tar.gz |
Merge pull request #8142 from ahaldane/ma_mean_scalar
BUG: np.ma.mean and var should return scalar if no mask
-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 44b64bdc8..c4a54acb4 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 be06e7230..a4887aaf0 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -3353,6 +3353,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 |