[Numpy-discussion] Unexpected MaskedArray behavior

Ryan May rmay31@gmail....
Wed Dec 17 10:57:03 CST 2008

Pierre GM wrote:
> On Dec 16, 2008, at 1:57 PM, Ryan May wrote:
>> I just noticed the following and I was kind of surprised:
>>>>> a = ma.MaskedArray([1,2,3,4,5], mask=[False,True,True,False,False])
>>>>> b = a*5
>>>>> b
>> masked_array(data = [5 -- -- 20 25],
>>       mask = [False  True  True False False],
>>       fill_value=999999)
>>>>> b.data
>> array([ 5, 10, 15, 20, 25])
>> I was expecting that the underlying data wouldn't get modified while  
>> masked.  Is
>> this actual behavior expected?
> Meh. Masked data shouldn't be trusted anyway, so I guess it doesn't  
> really matter one way or the other.
> But I tend to agree, it'd make more sense leave masked data untouched  
> (or at least, reset them to their original value after the operation),  
> which would mimic the behavior of gimp/photoshop.
> Looks like there's a relatively easy fix. I need time to check whether  
> it doesn't break anything elsewhere, nor that it slows things down too  
> much. I won't have time to test all that before next week, though. In  
> any case, that would be for 1.3.x, not for 1.2.x.
> In the meantime, if you need the functionality, use something like
> ma.where(a.mask,a,a*5)

I agree that masked values probably shouldn't be trusted, I was just surprised to 
see the behavior.  I just assumed that no operations were taking place on masked 

Just to clarify what I was doing here: I had a masked array of data, where the 
mask was set by a variety of different masked values.  Later on in the code, 
after doing some unit conversions, I went back to look at the raw data to find 
points that had one particular masked value set.  Instead, I was surprised to see 
all of the masked values had changed and I could no longer find any of the 
special values in the data.


Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

More information about the Numpy-discussion mailing list