Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding index of none and nan in numpy array

Tags:

python

nan

numpy

I have an array which looks like:

array([[  1.,   2.,   None],
       [ nan,   4.,   5.]])

I am trying the following:

np.equal(A, None) #works and finds index of None correctly
np.equal(A, np.nan) #doesn't work
np.isnan(A) #errors out

The error is:

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

How should I approach this, I am trying to find the index of None and nan in a given array.

My final output should look as:

array([[False, False,  True],
       [True, False, False]], dtype=bool)
like image 603
Zanam Avatar asked Mar 14 '17 19:03

Zanam


Video Answer


1 Answers

We could first cast the array to have dtype float — which will convert the None to NaN. numpy.isnan can then be used on this float array.

numpy.isnan(A.astype(float))
like image 93
kennytm Avatar answered Oct 14 '22 06:10

kennytm