[Numpy-discussion] array += masked_array gives a normal array, not masked array

Pierre GM pgmdevlist@gmail....
Sun Jun 1 20:28:04 CDT 2008


On Sunday 01 June 2008 11:22:05 Tony Yu wrote:
> array + masked_array
> outputs a masked array

That's expected: you create a new object from two objects (array and 
masked_array), the latter has a higher priority than the former, you end up 
with a masked array

> array += masked_array
> outputs an array.

That's expected as well: you modify the values of array in place, but don't 
modify its type. Similarly,
masked_array += array
gives a masked array.

> *However*, if the right side were an old masked array, the masked
> values would not be added to the output array. With the new masked
> arrays, the data from the masked array is added regardless of whether
> the value is masked (example, below).

With oldnumeric.ma, masked_array has to be converted to a ndarray. During the 
conversion, the masked data are filled with a value that depends on the 
context of the call: here, the function called is add, and the corresponding 
filling value is 0. Your line 
normal += masked_array
is then equivalent to
normal += [0,1]

With numpy.ma, masked_array is already recognized as a ndarray, and no 
conversion takes place. In particular, the values of masked_array are not 
filled.

You should use
normal += masked_array.filled(0)



More information about the Numpy-discussion mailing list