Python nan check

TL;DR

Use np.isnan (a) instead of a is np.nan.

nan

nan (Not A Number). The comparison, not the nan "number", is defined by IEEE754. This is not equal to anything. Even nan == nan is False. For Python, there are math.nan and np.nan. (There may be others.)

Some misconceptions: "You can check with a is np.nan "

If a is nan, it cannot be checked with a == np.nan. Because, as mentioned above, it is decided by IEEE754. So, what to do is that some sites say a is np.nan. Sure, this works in many cases, but apparently it just happens to work (it's supposed to refer to the same nan object) ** due to the implementation of numpy and math. So, I would like to conclude that it is basically the wrong check method.

That's why it's a counterexample.

#The following is Python 3.91 + numpy 1.19.I tried it in 4. It's basically the same with math nan.
import numpy as np
a = np.nan
b = a * 2
print(a)             # => nan
print(b)             # => nan
print(a is np.nan)   # => True (This is often used. This is ok)
print(b is np.nan)   # => False (This happens.)

Isn't it possible to calculate on nan? The result is still (of course?) Nan. If you compare with is here, it will be Flase because the reference destination seems to be different. So ** a is np.nan is wrong (as a check to see if it's nan) **.

What is the correct answer?

It's best to use np.isnan () obediently. It's easy to understand. As a trick (?) That took the definition in the wrong direction, there is also a comparison with itself a == a ( nan if is False), but I refrain from it because it is confusing. ..

Who is the bad guy? (It's an individual impression. Please don't mess around too much.)

It's Python (Bassari). The root of all evil is the introduction of things like is. This intuitive function causes ambiguous be verbs to play the important role of "comparison of references", causing confusion. To make matters worse, the is formula becomes widespread because it is easy to understand in English depending on how you read it (even if you misunderstand the meaning). Even if there is no is, there is aid (), so if you use it, it will be concise, accurate, and prevent misuse.

Recommended Posts

Python nan check
python grammar check
Domain check with Python
Check Python # type identity
Check version with python
Python
Check python coverage with pytest-cov
Python --Check type of values
Check OpenSSL version of python 2.6
[Python] Check the installed libraries
Error resolution python version check
Check for memory leaks in Python
[Python] Check the current directory, move the directory
The story of Python and the story of NaN
Check for external commands in python
Check the behavior when assigning Python
Check and move directories in Python
Check python code styles using pep8
Check installed modules from Python scripts
kafka python
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
[Python] Easy argument type check with dataclass
What is NaN? NaN Zoya (Python) (394 days late)
Check the behavior of destructor in Python
Python service
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Check the existence of the file with python
Python installation
Check and receive Serial port in Python (Port check)
python tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial