[Numpy-discussion] Setting element to masked in a masked array previously containing no masked values

Jesper Larsen jl at dmi.dk
Mon Jun 25 05:12:01 EDT 2007

Hi numpy users,

I have a masked array. I am looping over the elements of this array and 
sometimes want to set a value to missing. Normally this can be done by:

myarray.mask[i] = True

However the mask attribute is not indexable when there are no existing missing 
values in the array (it is simply False). In this case I therefore get an 
error message:

myarray.mask[i] = True
TypeError: object does not support item assignment

Is the best way to solve the problem to do something like this:

mask = ma.getmaskarray(myarray)
for i in range(n):
  if blahblah:
    mask[i] = True
myarray = ma.array(myarray, copy=False, mask=mask)

or is there a more elegant solution?

Does anyone by the way have any pointers to documentation of the masked array 
features of numpy? I know that it is treated in the numarray manual but it 
seems like there are some important syntax differences that make this manual 
of little use in that regard.

- Jesper

More information about the NumPy-Discussion mailing list