Anudeep K
Anudeep K

Reputation: 11

Unable to find nan value in numpy array even though it exists

The problem that I am facing

brands is a numpy array with 2314 elements. I am checking if there is a nan value in the array. The output shows false but when I tried intersection function with np.nan, it shows the common element as nan. So how come I cant find the nan value in array? And how do I remove it?

Upvotes: 1

Views: 1434

Answers (2)

Ynjxsjmh
Ynjxsjmh

Reputation: 29982

NaN is not equal with itself.

>>> np.nan != np.nan
True

You can use numpy.isnan to check

np.isnan(brands)

To remove nan, you can use

brands = brands[~np.isnan(brands)]

Upvotes: 3

Otto Hanski
Otto Hanski

Reputation: 394

The issue is that numpy's nan cannot be compared to itself, or in other words numpy.nan == numpy.nan returns False. Use instead numpy.isnan().

Upvotes: 4

Related Questions