I have the foll. masked array in numpy called arr with shape (50, 360, 720):
masked_array(data =
[[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
...,
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]],
mask =
[[ True True True ..., True True True]
[ True True True ..., True True True]
[ True True True ..., True True True]
...,
[ True True True ..., True True True]
[ True True True ..., True True True]
[ True True True ..., True True True]],
fill_value = 1e+20)
It has the foll. data in arr[0]:
arr[0].data
array([[-999., -999., -999., ..., -999., -999., -999.],
[-999., -999., -999., ..., -999., -999., -999.],
[-999., -999., -999., ..., -999., -999., -999.],
...,
[-999., -999., -999., ..., -999., -999., -999.],
[-999., -999., -999., ..., -999., -999., -999.],
[-999., -999., -999., ..., -999., -999., -999.]])
-999. is the missing_value and I want to replace it by 0.0. I do this:
arr[arr == -999.] = 0.0
However, arr remains the same even after this operation. How to fix this?
Maybe you want filled
. I'll illustrate:
In [702]: x=np.arange(10)
In [703]: xm=np.ma.masked_greater(x,5)
In [704]: xm
Out[704]:
masked_array(data = [0 1 2 3 4 5 -- -- -- --],
mask = [False False False False False False True True True True],
fill_value = 999999)
In [705]: xm.filled(10)
Out[705]: array([ 0, 1, 2, 3, 4, 5, 10, 10, 10, 10])
In this case filled
replaces all masked values with a fill value. Without an argument it would use the fill_value
.
np.ma
uses this approach to perform many of its calculations. For example its sum
is the same as if I filled all masked values with 0. prod
would replace them with 1.
In [707]: xm.sum()
Out[707]: 15
In [709]: xm.filled(0).sum()
Out[709]: 15
The result of filled
is a regular array, since all masked values have been replaced with something 'normal'.