[Numpy-discussion] Question about numpy.ma masking
Pierre GM
pgmdevlist at gmail.com
Fri May 7 16:28:55 EDT 2010
On May 4, 2010, at 8:38 PM, Gökhan Sever wrote:
> Hello,
>
> I have the following arrays read as masked array.
>
> I[10]: basic.data['Air_Temp'].mask
> O[10]: array([ True, False, False, ..., False, False, False], dtype=bool)
>
> [12]: basic.data['Press_Alt'].mask
> O[12]: False
>
> I[13]: len basic.data['Air_Temp']
> -----> len(basic.data['Air_Temp'])
> O[13]: 1758
>
>
> The first item data['Air_Temp'] has only the first element masked and this result with mask attribute being created an equal data length bool array. On the other hand data['Press_Alt'] has no elements to mask yielding a 'False' scalar. Is this a documented behavior or intentionally designed this way? This is the only case out of 20 that breaks my code as following: :)
>
> IndexError Traceback (most recent call last)
>
> 130 for k in range(len(shorter)):
> 131 if (serialh.data['dccnTempSF'][k] != 0) \
> --> 132 and (basic.data['Air_Temp'].mask[k+diff] == False):
> 133 dccnConAmb[k] = serialc.data['dccnConc'][k] * \
> 134 physical.data['STATIC_PR'][k+diff] * \
>
> IndexError: invalid index to scalar variable.
>
> since mask is a scalar in this case, nothing to loop terminating with an IndexError.
Gokhan,
Sorry for not getting back sooner, web connectivity was limited on my side.
I must admit I can't really see what you're tring to do here, but I'll throw some random comments:
* If you're using structured MaskedArrays, it's a really bad idea to call one of the fields "data", as it may interact in a non-obvious way with the actual "data" property (the one that outputs a view of the array as a pure ndarray).
* if you need to test whether an array has some masked elements, try something like
>>> myarray.mask is nomask
If True, no item is masked, the mask is a boolean and you can move on. If False, then the mask is a ndarray w/ as many elements as the array and you can index it.
More information about the NumPy-Discussion
mailing list