Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...