I would expect the result of a summation for a fully masked array to be zero, but instead "masked" is returned. How can I get the function to return zero?
>>> a = np.asarray([1, 2, 3, 4])
>>> b = np.ma.masked_array(a, mask=~(a > 2))
>>> b
masked_array(data = [-- -- 3 4],
mask = [ True True False False],
fill_value = 999999)
>>> b.sum()
7
>>> b = np.ma.masked_array(a, mask=~(a > 5))
>>> b
masked_array(data = [-- -- -- --],
mask = [ True True True True],
fill_value = 999999)
>>> b.sum()
masked
>>> np.ma.sum(b)
masked
>>>
Here's another unexpected thing:
>>> b.sum() + 3
masked
In your last case:
If I specify
keepdims
, I get a different array:here's the relevant part of the
sum
code:It's the
lines that produce the
masked
value in your case.newmask
is True in the case where all values are masked, and False is some are not. The choice to returnnp.ma.masked
is deliberate.The core of the calculation is:
the rest of the code decides whether to return a scalar or masked array.
============
And for your addition:
It looks like the
np.ma.masked
is a special array that propagates itself across calculations. Sort of likenp.nan
.