[Numpy-discussion] Operations on masked items

Ryan May rmay31@gmail....
Tue Feb 3 15:00:28 CST 2009

Ryan May wrote:
> Pierre,
> I know you did some preliminary work on helping to make sure that doing
> operations on masked arrays doesn't change the underlying data.  I ran into the
> following today.
> import numpy as np
> a = np.ma.array([1,2,3], mask=[False, True, False])
> b = a * 10
> c = 10 * a
> print b.data # Prints [10 2 30] Good!
> print c.data # Prints [10 10 30] Oops.
> I tracked it down to __call__ on the _MaskedBinaryOperation class.  If there's a
> mask on the data, you use:
> 	result = np.where(m, da, self.f(da, db, *args, **kwargs))
> You can see that if a (and hence da) is a scalar, your masked values end up with
> the value of the scalar.  If this is getting too hairy to handle not touching
> data, I understand.  I just thought I should point out the inconsistency here.

Well, I guess I hit send too soon.  Here's one easy solution (consistent with
what you did for __radd__), change the code for __rmul__ to do:

	return multiply(self, other)

instead of:

	return multiply(other, self)

That fixes it for me, and I don't see how it would break anything.


Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

More information about the Numpy-discussion mailing list