summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/ma/core.py12
-rw-r--r--numpy/ma/tests/test_core.py16
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