abudis
abudis

Reputation: 2881

Substituting values in a numpy masked array

I'm trying to substitute some values in a numpy masked array, but my mask is being dropped:

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

The result is:

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

But what I want is:

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

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

Upvotes: 4

Views: 3193

Answers (1)

jabaldonedo
jabaldonedo

Reputation: 26602

I think you are not doing the substitution correctly, try this:

>>> 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)

Upvotes: 4

Related Questions