0

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?

3
  • You may have changed the data for the array, but you didn't change the mask. Commented Jul 8, 2016 at 1:42
  • thanks @hpaulj, how do I fix it? Commented Jul 8, 2016 at 1:46
  • I had tried using ma.set_fill_value but that did not seem to work either Commented Jul 8, 2016 at 1:50

1 Answer 1

2

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'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.