# Replace NaNs in masked numpy array

809
January 05, 2017, at 08:38 AM
``````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]
[[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
...,
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]],
[[ 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?

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.])
``````
POPULAR ONLINE

### How to connect oracle database with python pypyodbc

I am trying to connect an oracle database from my python code using pypyodbc:

1405

### Testing machine learning Python and R for Spark on my local machine? [on hold]

I would like to test some machine learning scripts written in Python using Pandas and Scikit learn and in RI want to see if I can rewrite them using PySpark MLLib and SparkR

272

### Django-tables2 table renders empty when coulmns are created dynamically

I defined django-tables2 Table class with Column fields set dynamically:

386

### Swapping items in container - evaluation order

Edited for clarity:

318