Substituting values ​​into a masked array in numpy

I try to substitute some values ​​in numpy masked array, but my mask is discarded:

import numpy as np
a = np.ma.array([1, 2, 3, -1, 5], mask=[0, 0, 0, 1, 0])
a[a < 2] = 999

Result:

masked_array(data = [999 2 3 999 5],
mask = [False False False False False],
fill_value = 999999)

But I want:

masked_array(data = [999 2 3 -- 5],
mask = [False False False  True False],
fill_value = 999999)

What am I doing wrong? I am using Python 2.7 and numpy 1.7.1 on Ubuntu 13.10

+4
source share
1 answer

I think you are doing the wrong substitution, try the following:

>>> import numpy as np
>>> a = np.ma.array([1, 2, 3, -1, 5], mask=[0, 0, 0, 1, 0])
>>> a.data[a < 2] = 999
>>> a
 masked_array(data = [999 2 3 -- 5],
         mask = [False False False  True False],
   fill_value = 999999)
+4
source

All Articles