On Sat, May 8, 2010 at 9:29 PM, Eric Firing <efir...@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<gokhanse...@gmail.com>
>  wrote:
> >> Hello,
> >>
> >> Consider my masked arrays:
> >>
> >> I[28]: type basic.data['Air_Temp']
> >> ----->  type(basic.data['Air_Temp'])
> >> O[28]: numpy.ma.core.MaskedArray
> >>
> >> I[29]: basic.data['Air_Temp']
> >> O[29]:
> >> masked_array(data = [-- -- -- ..., -- -- --],
> >>               mask = [ True  True  True ...,  True  True  True],
> >>         fill_value = 999999.9999)
> >>
> >>
> >> I[17]: basic.data['Air_Temp'].data =
> np.ones(len(basic.data['Air_Temp']))*30
> >>
> ---------------------------------------------------------------------------
> >> AttributeError                            Traceback (most recent call
> last)
> >>
> >> ---->  1
> >>        2
> >>        3
> >>        4
> >>        5
> >>
> >> 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:
>
> In [1]:x = np.ma.array([1,2,3], mask=[True, True, True], dtype=float)
>
> In [2]:x
> Out[2]:
> masked_array(data = [-- -- --],
>              mask = [ True  True  True],
>        fill_value = 1e+20)
>
>
> In [3]:x[:] = 30
>
> In [4]:x
> Out[4]:
> masked_array(data = [30.0 30.0 30.0],
>              mask = [False False False],
>        fill_value = 1e+20)
>
>
> In [5]:x[:] = np.ma.masked
>
> In [6]:x
> Out[6]:
> masked_array(data = [-- -- --],
>              mask = [ True  True  True],
>        fill_value = 1e+20)
>
>
> In [7]:x.data
> Out[7]:array([ 30.,  30.,  30.])
>
>
> Eric
>
> >
> > Ryan
> >
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

Good to see this :)

I[45]: x = np.ma.array([1,2,3], mask=[True, True, True], dtype=float)

I[46]: x
O[46]:
masked_array(data = [-- -- --],
             mask = [ True  True  True],
       fill_value = 1e+20)


I[47]: x.data[:] = 25

I[48]: x
O[48]:
masked_array(data = [-- -- --],
             mask = [ True  True  True],
       fill_value = 1e+20)


I[49]: x[:] = 25

I[50]: x
O[50]:
masked_array(data = [25.0 25.0 25.0],
             mask = [False False False],
       fill_value = 1e+20)


I was also updating mask values after updating data attribute. Now setting
the masked array itself to a number automatically flips the masks for me
which is very useful. I check if a valid temperature exists, otherwise
assign my calculation to another missing value.

-- 
Gökhan
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to