>> 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?
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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With