[Numpy-discussion] Question about masked arrays

Ryan May rmay31@gmail....
Mon Sep 20 15:34:23 CDT 2010

On Mon, Sep 20, 2010 at 3:23 PM, Gökhan Sever <gokhansever@gmail.com> wrote:
> On Mon, Sep 20, 2010 at 1:05 PM, Robert Kern <robert.kern@gmail.com> wrote:
>> Are you asking about when masked arrays are casted to ndarrays (and
>> thus losing the mask information)? Most times when a function uses
>> asarray() or array() to explicitly cast the inputs to an ndarray. The
>> reason that np.mean() gives the same result as np.ma.mean() is that it
>> simply defers to the .mean() method on the object, so it works as
>> expected on a masked array. Many other functions will not.
>> --
>> Robert Kern
> Right guess. It is important for me to able to preserve masked array
> properties of an array. Otherwise losing the mask information yields
> unexpected results in some of my calculations. I could see from np.mean??
> that mean function is indeed the object method. Also in /numpy/ma there is a
> conversion for np.zeros(). I guess in any case it is the user's
> responsibility to make sure that the operations are performed on a desired
> array type.

True, but in some cases the functions just blindly call asarray() or
array() without thinking about using asanyarray(). If you encounter a
basic numpy function that calls asarray() but would work fine with
masked arrays (or other subclasses), feel free to file/post as a bug.
It's good to get those cases fixed where possible. (I've done this in
the past.)


Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

More information about the NumPy-Discussion mailing list