"isnotnan" functionality in numpy, can this be more pythonic?

AnalyticsBuilder picture AnalyticsBuilder · May 14, 2010 · Viewed 78.6k times · Source

I need a function that returns non-NaN values from an array. Currently I am doing it this way:

>>> a = np.array([np.nan, 1, 2])
>>> a
array([ NaN,   1.,   2.])

>>> np.invert(np.isnan(a))
array([False,  True,  True], dtype=bool)

>>> a[np.invert(np.isnan(a))]
array([ 1.,  2.])

Python: 2.6.4 numpy: 1.3.0

Please share if you know a better way, Thank you

Answer

mtrw picture mtrw · May 14, 2010
a = a[~np.isnan(a)]