🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.1 Manual
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. ... >>> import numpy as np >>> np.isnan(np.nan) True >>> np.isnan(np.inf) False >>> np.isnan([np.log(-1.),1.,np.log(0)]) array([ True, False, False])
Discussions

python - Numpy is not setting properly nan values: arr[arr== 0] = np.nan - Geographic Information Systems Stack Exchange
I am creating a script to normalize a satellite scene. My input image is of type float32, and no NoData value is assigned. However, during the normalization, I want to avoid using pixels with a val... More on gis.stackexchange.com
🌐 gis.stackexchange.com
python - numpy NaN not always recognized - Stack Overflow
It's perfectly valid for x is y to be true for two separate NaN values, or for it to be false; your code cannot rely on either. It may be interesting learning why it is and isn't true for different cases (depending on your numpy and Python versions), but it's not useful except in getting to ... More on stackoverflow.com
🌐 stackoverflow.com
math.nan and numpy.nan
NAN by IEEE-744 standard is not equal to any other float value, including other NAN, infinities etc. import math print(math.nan == math.nan) # False To check if a floating point value is NAN, you can use the isnan function: import math import numpy print(math.isnan(math.nan)) # True print(numpy.isnan(numpy.nan)) # True More on reddit.com
🌐 r/learnpython
2
2
January 16, 2023
NumPy 2.0.0 is the first major release since 2006.
This is an example of a good governing model for open source libraries. Design your public APIs in such a way that there should be no breaking API changes in a short span of time and there should be minimum LTS branches to maintain. It allows industrial projects to catch up with most of your features and documentation. Then years later you finally revisit your legacy APIs, redesign them and move to version 2 while also maintaining backward compatibility. SQLAlchemy is another library that is built right. I discourage packages which goes from version 1 to version 6+ in a matter of 2 years. It creates too much fragmentation and users are not able to keep up to date with new APIs. High version number should not be seen as an indicator of rapid development. More on reddit.com
🌐 r/Python
60
588
July 17, 2024
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.5 Manual
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. ... Try it in your browser! >>> import numpy as np >>> np.isnan(np.nan) True >>> np.isnan(np.inf) False >>> np.isnan([np.log(-1.),1.,np.log(0)]) array([ ...
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.6.dev0 Manual
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. ... Try it in your browser! >>> import numpy as np >>> np.isnan(np.nan) True >>> np.isnan(np.inf) False >>> np.isnan([np.log(-1.),1.,np.log(0)]) array([ ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › counting-the-number-of-non-nan-elements-in-a-numpy-array
Counting the number of non-NaN elements in a NumPy Array - GeeksforGeeks
July 23, 2025 - In this article, we are going to see how to count the number of non-NaN elements in a NumPy array in Python. NAN: It is used when you don't care what the value is at that position.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Remove NaN (np.nan) from an array | note.nkmk.me
January 23, 2024 - note.nkmk.me · Home · Python ... (np.nan) from an array (ndarray), use np.isnan() to identify NaN and methods like any() or all() to extract rows or columns that do not contain NaN....
🌐
Saturn Cloud
saturncloud.io › blog › efficiently-checking-if-arbitrary-object-is-nan-in-python-numpy-pandas
Efficiently Checking if Arbitrary Object is NaN in Python Numpy and Pandas | Saturn Cloud Blog
May 1, 2026 - In conclusion, checking for NaN values is a common task in data science and software engineering. In Python, the math module’s isnan() function can be used for floating-point numbers, while the numpy library’s isnan() function can handle NaN values for different data types.
Find elsewhere
🌐
IncludeHelp
includehelp.com › python › isnotnan-functionality-in-numpy-can-this-be-more-pythonic.aspx
Python - 'isnotnan' functionality in numpy, can this be more pythonic?
October 9, 2023 - To check if a value is not a nan value, we will use isnan() method along with tilde(~) operator. The tilde sign (~) in pandas is used when we work with Boolean values. In programming, we sometimes use some specific values that only have two values, either True or False.
Top answer
1 of 3
19

This isn't so much a question about the Python is operator, as about what indexing, or unboxing, an element of an array does:

In [363]: a = np.array([1, 2, np.nan, 3])

In [364]: a[2]
Out[364]: nan

In [365]: type(a[2])
Out[365]: numpy.float64

In [366]: a[2] is a[2]
Out[366]: False

a[2] doesn't simply return nan. It returns a np.float64 object whose values is np.nan. Another a[2] will produce another np.float64 object. Two such objects don't match in the is sense. That's true for any array element, not just nan values.

Since == doesn't work for nan, we are stuck with using the np.isnan function.

np.nan is a unique float object (in this session), but a[2] is not set to that object.

If the array was defined as an object type:

In [376]: b = np.array([1, 2, np.nan, 3], object)

In [377]: b[2] is np.nan
Out[377]: True

here the is is True - because b contains pointers to objects that already exist in memory, including the np.nan object. Same would be true for a list constructed like that.

2 of 3
7

First, at least in NumPy 1.15, np.nan happens to be a special singleton, meaning that whenever NumPy has to give you a NaN value of type float, it tries to give you the same np.nan value.

But this is not documented anywhere, or guaranteed to be true across versions.

This fits into the larger class of values that may or may not be singletons, as an implementation detail.

As a general rule, if your code relies on two equal values of an immutable type being identical or not being identical, your code is wrong.

Here are some examples from a default build of CPython 3.7:

>>> a, b = 200, 201
>>> a is b-1
True
>>> a, b = 300, 301
>>> a is b-1
False
>>> 301-1 is 300
True
>>> math.nan is math.nan
True
>>> float('nan') is math.nan
False
>>> float('nan') is float('nan')
False

You can learn all of the rules that make all of these things come out that way, but they could all change in a different Python implementation, or in version 3.8, or even in 3.7 built with custom configure options. So, just never use 1 or math.nan or np.nan or '' with is; only use it for objects that are specifically documented to be singletons (like None—or instances of your own types, of course).


Second, when you index a numpy array, it has to "unbox" the value by constructing a scalar, of a type appropriate to the array's dtype. For a dtype=float64 array, the scalar value it constructs is a np.float64.

So, a[2] is guaranteed to be a np.float64.

But np.nan is not an np.float64, it's a float.

So, there's no way NumPy can give you np.nan when you ask for a[2]. Instead, it gives you an np.float64 with a NaN value.


OK, so that's why a[2] is np.nan is always False. But why is a[2] is a[2] also usually false?

As I mentioned above, NumPy tries to give you np.nan whenever it needs to give you a float NaN. But—at least in 1.15—it doesn't have any special singleton value to provide whenever it needs to give you a np.float64 NaN. There's no reason it couldn't, but nobody bothered to write such code, because it shouldn't matter either way to any properly-written app.

So, each time you unbox the value in a[2] into a scalar np.float64, it gives you a new NaN-valued np.float64.

But why isn't this the same as 301-1 is 300? Well, the reason that works is that the compiler is allowed to fold constants of known immutable type with equal values, and CPython does exactly that, for simple cases, within each compilation unit. But two NaN values aren't equal; a NaN value isn't even equal to itself. So, it can't be constant-folded.

(If you're wondering what happens if you create an array with an int dtype and store small values in it and check whether they get merged into the small-int singletons—try it and see.)


And of course this is why isnan exists in the first place. You can't test for NaN with equality (because NaN values are not equal to anything, even themselves), you can't test for NaN with identity (for all of the reasons described above), so you need a function to test for them.

🌐
AskPython
askpython.com › python › examples › nan-in-numpy-and-pandas
Understanding np.nan in NumPy and Pandas - AskPython
September 30, 2026 - NaN means not a number and is a floating-point value that NumPy uses for an unavailable numeric value.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.0 Manual
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. ... >>> np.isnan(np.nan) True >>> np.isnan(np.inf) False >>> np.isnan([np.log(-1.),1.,np.log(0)]) array([ True, False, False])
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.2 Manual
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. ... >>> import numpy as np >>> np.isnan(np.nan) True >>> np.isnan(np.inf) False >>> np.isnan([np.log(-1.),1.,np.log(0)]) array([ True, False, False])
🌐
SciPy
docs.scipy.org › doc › numpy-1.10.1 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v1.10 Manual
Test element-wise for NaN and return result as a boolean array. ... Numpy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity.
🌐
Note.nkmk.me
note.nkmk.me › home › python
What is nan in Python (float('nan'), math.nan, np.nan) | note.nkmk.me
May 9, 2023 - In addition, nan is defined in math (standard library) and NumPy; both NaN and NAN are defined as aliases in NumPy. math.nan — Mathematical functions — Python 3.11.3 documentation ...
🌐
NumPy
numpy.org › doc › stable › reference › constants.html
Constants — NumPy v2.5 Manual
Also that positive infinity is not equivalent to negative infinity. But infinity is equivalent to positive infinity. Examples · Try it in your browser! >>> import numpy as np >>> np.inf inf >>> np.array([1]) / 0. array([inf]) Go BackOpen In Tab · numpy.nan# IEEE 754 floating point representation of Not a Number (NaN).
🌐
NumPy
numpy.org › doc › stable › user › misc.html
Miscellaneous — NumPy v2.5 Manual
isnan - True if value is nan · isinf - True if value is inf · isfinite - True if not nan or inf · nan_to_num - Map nan to 0, inf to max float, -inf to min float · The following corresponds to the usual functions except that nans are excluded from the results: nansum ·
🌐
Medium
medium.com › @dnaresh2323 › why-nan-nan-is-false-deep-dive-into-pythons-missing-values-dba2bf9452f6
Why NaN == NaN is False? Deep Dive into Python’s Missing Values | by D Naresh | Medium
October 10, 2025 - For example, when Python or NumPy cannot calculate a number properly, it returns NaN instead of throwing an error. ... NaN is not equal to 0, not equal to None, not equal to any number.
🌐
SciPy
docs.scipy.org › doc › numpy-1.9.2 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v1.9 Manual
Test element-wise for NaN and return result as a boolean array. ... Numpy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity.
🌐
Reddit
reddit.com › r/learnpython › math.nan and numpy.nan
r/learnpython on Reddit: math.nan and numpy.nan
January 16, 2023 -

I ran into something puzzling while trying to identify nan's in a matrix. I ran some code in the python console and am just wondering why the output is what it is.

import math
a = math.nan
print(a)
nan
import numpy
b = numpy.nan
print(b)
nan
a == b
False

If a is nan and b is nan, why aren't they seen as the same? I get that the numpy and math modules are different but what is the computer comparing to get False as an output?