8000 BUG: np.ma.mean and var should return scalar if no mask by ahaldane · Pull Request #8142 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: np.ma.mean and var should return scalar if no mask #8142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5057,7 +5057,7 @@ def mean(self, axis=None, dtype=None, out=None, keepdims=np._NoValue):

if self._mask is nomask:
result = super(MaskedArray, self).mean(axis=axis,
dtype=dtype, **kwargs)
dtype=dtype, **kwargs)[()]
else:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the super mean returns a 0-d array scalar for relevant cases. I note the following odd behavior of ordinary arrays that is new to me.

>>> np.float64(1)
1.0
>>> np.float64(1)[()]
array(1.0)
>>> np.float64(1)[()][()]
1.0

That is, the [()] construct flips the result back and forth between arrays and scalars.

Copy link
Member
@eric-wieser eric-wieser Feb 28, 2017
10000

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For anyone coming here in future, the above is no longer (1.11.1) the case. [()] does not promote scalars to 0d-arrays

dsum = self.sum(axis=axis, dtype=dtype, **kwargs)
cnt = self.count(axis=axis, **kwargs)
Expand Down Expand Up @@ -5134,8 +5134,14 @@ def var(self, axis=None, dtype=None, out=None, ddof=0,

# 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)
Expand Down
16 changes: 16 additions & 0 deletions numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3340,6 +3340,22 @@ def test_dot_shape_mismatch(self):
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
Expand Down
0