[Numpy-discussion] easy way to change part of only unmasked elements value?

Chao YUE chaoyuejoy at gmail.com
Sat Sep 15 11:20:30 EDT 2012


but I think I personally prefer the reverse.
I would expect when I do
a[3:6]=1
the mask state would not change.

then I want to change the "base", I would use a.base[3:6]=1
then the mask state would change also.

By the way, I found b.data always be equal to b.base?

cheers,

Chao

On Tue, Sep 11, 2012 at 5:24 PM, Chao YUE <chaoyuejoy at gmail.com> wrote:

> Dear Richard,
>
> this is what I want. Thanks!
>
> Chao
>
>
> On Tue, Sep 11, 2012 at 3:19 PM, Richard Hattersley <rhattersley at gmail.com
> > wrote:
>
>> Hi Chao,
>>
>> If you don't mind modifying masked values, then if you write to the
>> underlying ndarray it won't touch the mask:
>>
>> >>> a = np.ma.masked_less(np.arange(10),5)
>> >>> a.base[3:6] = 1
>> >>> a
>>
>> masked_array(data = [-- -- -- -- -- 1 6 7 8 9],
>>              mask = [ True  True  True  True  True False False False
>> False False],
>>        fill_value = 999999)
>>
>> Regards,
>> Richard Hattersley
>>
>>
>> On 10 September 2012 17:43, Chao YUE <chaoyuejoy at gmail.com> wrote:
>>
>>> Dear all numpy users,
>>>
>>> what's the easy way if I just want to change part of the unmasked array
>>> elements into another new value? like an example below:
>>> in my real case, I would like to change a subgrid of a masked numpy
>>> array to another value, but this grid include both masked and unmasked data.
>>> If I do a simple array[index1:index2, index3:index4] = another_value,
>>> those data with original True mask will change into False. I am using numpy
>>> 1.6.2.
>>> Thanks for any ideas.
>>>
>>> In [91]: a = np.ma.masked_less(np.arange(10),5)
>>>
>>> In [92]: or_mask = a.mask.copy()
>>> In [93]: a
>>> Out[93]:
>>> masked_array(data = [-- -- -- -- -- 5 6 7 8 9],
>>>              mask = [ True  True  True  True  True False False False
>>> False False],
>>>        fill_value = 999999)
>>>
>>>
>>> In [94]: a[3:6]=1
>>>
>>> In [95]: a
>>> Out[95]:
>>> masked_array(data = [-- -- -- 1 1 1 6 7 8 9],
>>>              mask = [ True  True  True False False False False False
>>> False False],
>>>        fill_value = 999999)
>>>
>>>
>>> In [96]: a = np.ma.masked_array(a,mask=or_mask)
>>>
>>> In [97]: a
>>> Out[97]:
>>> masked_array(data = [-- -- -- -- -- 1 6 7 8 9],
>>>              mask = [ True  True  True  True  True False False False
>>> False False],
>>>        fill_value = 999999)
>>>
>>> Chao
>>>
>>> --
>>>
>>> ***********************************************************************************
>>> Chao YUE
>>> Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
>>> UMR 1572 CEA-CNRS-UVSQ
>>> Batiment 712 - Pe 119
>>> 91191 GIF Sur YVETTE Cedex
>>> Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
>>>
>>> ************************************************************************************
>>>
>>>
>>> _______________________________________________
>>> NumPy-Discussion mailing list
>>> NumPy-Discussion at scipy.org
>>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>>
>>>
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
>
>
> --
>
> ***********************************************************************************
> Chao YUE
> Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
> UMR 1572 CEA-CNRS-UVSQ
> Batiment 712 - Pe 119
> 91191 GIF Sur YVETTE Cedex
> Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
>
> ************************************************************************************
>
>


-- 
***********************************************************************************
Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120915/56a36e70/attachment.html>


More information about the NumPy-Discussion mailing list