1

I have the foll. numpy masked array:

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

I want to replace all the -9999.0 in this masked array with 0.0, but the foll. does not work:

arr.data[arr == -9999.0] = 0.0

The resulting arr still has all the -9999.0 in it. How do I fix it?

--EDIT:

This is what arr.data looks like:

array([[-9999., -9999., -9999., ..., -9999., -9999., -9999.],
       [-9999., -9999., -9999., ..., -9999., -9999., -9999.],
       [-9999., -9999., -9999., ..., -9999., -9999., -9999.],
       ..., 
       [-9999., -9999., -9999., ..., -9999., -9999., -9999.],
       [-9999., -9999., -9999., ..., -9999., -9999., -9999.],
       [-9999., -9999., -9999., ..., -9999., -9999., -9999.]], dtype=float32)
4
  • Are you sure it has -9999.0 in it, and not some other floats very close to -9999.0? Commented Jan 19, 2017 at 23:07
  • thanks @wim, I am absolutely sure it is -9999.0 Commented Jan 19, 2017 at 23:07
  • @wim, edited question to show what arr.data looks like Commented Jan 19, 2017 at 23:08
  • 1
    Do you also want to change the mask? arr.filled(0) might be what you want. Commented Jan 20, 2017 at 0:50

1 Answer 1

3

The mask is on where the arr.mask has value True.

All those values -9999. values are masked.

If you want it to apply to the masked values aswell, instead of using this:

arr.data[arr == -9999.0] = 0.0

It should be this:

arr.data[arr.data == -9999.0] = 0.0

Note: Be careful with float equality comparisons like this. Usually you want comparison within a tolerance, instead of direct equality. See np.isclose to read more details about that.

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.