Skip to content
Advertisement

replace masked with nan in numpy masked_array

>> masks = [[1,1],[0,0]]    
>> [np.ma.masked_array(data=np.array([1.0,2.0]), mask=m, fill_value=np.nan).mean() for m in masks]
   [masked, 1.5]

I’d like to replace the masked result with nan. Is there a way to do that directly with numpy’s masked_array?

Advertisement

Answer

In [232]: M = np.ma.masked_array(data=np.array([1.0,2.0]),mask=[True, False])

filled method replaces the masked values with the fill value:

In [233]: M.filled()                                                         
Out[233]: array([1.e+20, 2.e+00])
In [234]: M.filled(np.nan)         # or with a value of your choice.                                                   
Out[234]: array([nan,  2.])

Or as you do, specify the fill value when defining the array:

In [235]: M = np.ma.masked_array(data=np.array([1.0,2.0]),mask=[True, False],
     ...:  fill_value=np.nan)                                                
In [236]: M                                                                  
Out[236]: 
masked_array(data=[--, 2.0],
             mask=[ True, False],
       fill_value=nan)
In [237]: M.filled()                                                         
Out[237]: array([nan,  2.])

The masked mean method skips over the filled values:

In [238]: M.mean()                                                           
Out[238]: 2.0
In [239]: M.filled().mean()                                                  
Out[239]: nan
In [241]: np.nanmean(M.filled())    # so does the `nanmean` function
In [242]: M.data.mean()             # mean of the underlying data                                                      
Out[242]: 1.5
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement