[Numpy-discussion] Another masked array question

Pierre GM pgmdevlist at gmail.com
Sun May 9 20:16:32 EDT 2010


On May 8, 2010, at 9:51 PM, Gökhan Sever wrote:
> 
> 
> 
> On Sat, May 8, 2010 at 9:29 PM, Eric Firing <efiring at hawaii.edu> wrote:
> On 05/08/2010 04:16 PM, Ryan May wrote:
> > On Sat, May 8, 2010 at 7:52 PM, Gökhan Sever<gokhansever at gmail.com>  wrote:
> >>
> >> AttributeError: can't set attribute
> >>
> >> Why this assignment fails? I want to set each element in the original
> >> basic.data['Air_Temp'].data to another value. (Because the main instrument
> >> was forgotten to turn on for that day, and I am using a secondary
> >> measurement data for Air Temperature for my another calculation. However it
> >> fails. Although single assignment works:
> >>
> >> I[13]: basic.data['Air_Temp'].data[0] = 30
> >>
> >> Shouldn't this be working like the regular NumPy arrays do?
> >
> > Based on the traceback, I'd say it's because you're trying to replace
> > the object pointed to by the .data attribute. Instead, try to just
> > change the bits contained in .data:
> >
> > basic.data['Air_Temp'].data[:] = np.ones(len(basic.data['Air_Temp']))*30
> 
> Also, you since you are setting all elements to a single value, you
> don't need to generate an array on the right-hand side.  And, you don't
> need to manipulate ".data" directly--I think it is best to avoid doing
> so.  Consider:

Yep. The "data" attribute is in fact a read-only property that retuns a view of the masked array as a standard ndarray. If you need to set individual values, just do so on the masked array. If you need to mask a value, use the syntax
>>> yourarray[yourindex] = np.ma.masked





More information about the NumPy-Discussion mailing list