[Numpy-discussion] Another masked array question
Eric Firing
efiring at hawaii.edu
Sat May 8 22:29:01 EDT 2010
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:
>> 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
>
More information about the NumPy-Discussion
mailing list