0
var[np.isnan(var)] = 0.0

var.shape
(50, 360, 720)

I want to replace NaNs in the array with 0.0. However, I get an error: *** IndexError: Index cannot be multidimensional

var[0]

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

How to fix it?

1 Answer 1

1

nan_to_num is the right way to do it. Here's a 2D example:

In [25]: arr = np.array([[10, 20], [np.nan, 30], [np.nan, -10]])

In [26]: arr
Out[26]: 
array([[ 10.,  20.],
       [ nan,  30.],
       [ nan, -10.]])

In [27]: np.nan_to_num(arr)
Out[27]: 
array([[ 10.,  20.],
       [  0.,  30.],
       [  0., -10.]])

I believe nan_to_num should work on masked arrays as well. For example:

In [33]: mx = ma.masked_array([np.nan, 2, 3, 4], mask=[0, 0, 1, 0])

In [34]: mx.compressed()
Out[34]: array([ nan,   2.,   4.])

And now:

In [36]: np.nan_to_num(ma.masked_array([np.nan, 2, 3, 4], mask=[0, 0, 1, 0])).compressed()
Out[36]: array([ 0.,  2.,  4.])
Sign up to request clarification or add additional context in comments.

3 Comments

@user308827: it really shouldn't. what makes you think so?
In my full dataset, it is converting everything to 0.0
@user308827: see my update to the answer. Maybe something is wrong with your dataset (all NaNs?) -- check it step by step

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.