Zanam
Zanam

Reputation: 4807

finding index of none and nan in numpy array

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)

Upvotes: 7

Views: 7541

Answers (1)

kennytm
kennytm

Reputation: 523184

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

Upvotes: 6

Related Questions