Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does assert np.nan == np.nan cause an error?

Tags:

python

numpy

If

assert 1 == 1

is fine, then why does:

assert np.nan == np.nan

cause an assertion error?

What's even more confusing, this is OK:

assert np.nan != np.nan

What's the best way to test for nan?

like image 922
cjm2671 Avatar asked Jun 05 '17 11:06

cjm2671


People also ask

How do I fix NaN error in Python?

We can replace NaN values with 0 to get rid of NaN values. This is done by using fillna() function. This function will check the NaN values in the dataframe columns and fill the given value.

What causes NaN in Python?

Nan means “Not a number”, this is because inside your cube function, you're not calling the square function, but getting it's contents.

What is the meaning of NP NaN?

Introduction to NumPy NaN. In Python, NumPy NAN stands for not a number and is defined as a substitute for declaring value which are numerical values that are missing values in an array as NumPy is used to deal with arrays in Python and this can be initialized using numpy.

Is NP NaN false?

nan is np. nan is True and one is two is also True . If you check the id of one and two using id(one) and id(two) , the same id will be displayed.


1 Answers

NaN has the property that it doesn't equal itself, you should use np.isnan to test NaN values, here np.isnan(np.nan) will yield True:

In[5]:
np.nan == np.nan

Out[5]: False

In[6]:
np.nan != np.nan

Out[6]: True

In[7]:
np.isnan(np.nan)

Out[7]: True
like image 79
EdChum Avatar answered Sep 29 '22 20:09

EdChum