Ray's solution is good. However, on my machine it is about 2.5x faster to use numpy.sum in place of numpy.min:

In [13]: %timeit np.isnan(np.min(x))
1000 loops, best of 3: 244 us per loop

In [14]: %timeit np.isnan(np.sum(x))
10000 loops, best of 3: 97.3 us per loop

Unlike min, sum doesn't require branching, which on modern hardware tends to be pretty expensive. This is probably the reason why sum is faster.

edit The above test was performed with a single NaN right in the middle of the array.

It is interesting to note that min is slower in the presence of NaNs than in their absence. It also seems to get slower as NaNs get closer to the start of the array. On the other hand, sum's throughput seems constant regardless of whether there are NaNs and where they're located:

In [40]: x = np.random.rand(100000)

In [41]: %timeit np.isnan(np.min(x))
10000 loops, best of 3: 153 us per loop

In [42]: %timeit np.isnan(np.sum(x))
10000 loops, best of 3: 95.9 us per loop

In [43]: x[50000] = np.nan

In [44]: %timeit np.isnan(np.min(x))
1000 loops, best of 3: 239 us per loop

In [45]: %timeit np.isnan(np.sum(x))
10000 loops, best of 3: 95.8 us per loop

In [46]: x[0] = np.nan

In [47]: %timeit np.isnan(np.min(x))
1000 loops, best of 3: 326 us per loop

In [48]: %timeit np.isnan(np.sum(x))
10000 loops, best of 3: 95.9 us per loop
Answer from NPE on Stack Overflow
🌐
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 ... regardless of the method used for creation. You can check if a value is nan or not with math.isnan()....
People also ask

Is np.nan a float?
Yes. np.nan is a floating-point value, so including it in a NumPy array of integers causes the array to use a floating-point dtype.
🌐
askpython.com
askpython.com › python › examples › nan-in-numpy-and-pandas
Understanding np.nan in NumPy and Pandas - AskPython
Can I sum an array that contains np.nan?
Yes, but a regular NumPy sum returns NaN when a NaN participates. Use np.nansum only when ignoring missing entries is valid for the calculation.
🌐
askpython.com
askpython.com › python › examples › nan-in-numpy-and-pandas
Understanding np.nan in NumPy and Pandas - AskPython
What does np.nan do?
np.nan is a floating-point NaN value used to mark an unavailable numeric entry. Use np.isnan to detect it in a NumPy array.
🌐
askpython.com
askpython.com › python › examples › nan-in-numpy-and-pandas
Understanding np.nan in NumPy and Pandas - AskPython
🌐
Finxter
blog.finxter.com › home › learn python blog › check for nan values in python
Check for NaN Values in Python - Be on the Right Side of Change
April 2, 2022 - import numpy as np arr = np.array([10, 20, np.nan, 40, np.nan]) for x in arr: if np.isnan(x): print("Not a Number!") else: print(x, ":", type(x)) ... 10.0 : <class 'numpy.float64'> 20.0 : <class 'numpy.float64'> Not a Number!
🌐
Codegive
codegive.com › blog › numpy_float64_nan_check.php
Numpy float64 nan check
When you explicitly create np.nan or a computation results in NaN in NumPy, it is typically of type numpy.float64 (double-precision floating-point).
🌐
GeeksforGeeks
geeksforgeeks.org › python › check-for-nan-values-in-python
Check For NaN Values in Python - GeeksforGeeks
July 23, 2025 - NaN values are commonly encountered in data science and numerical computing when working with datasets that contain missing, undefined, or invalid values. The numpy.isnan() function is a simple way to check for NaN values in a NumPy array.
Find elsewhere
🌐
AskPython
askpython.com › python › examples › nan-in-numpy-and-pandas
Understanding np.nan in NumPy and Pandas - AskPython
September 30, 2026 - NumPy promotes this array to float64 because np.nan is a floating-point value and the other entries are integers.
🌐
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 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.
🌐
Index.dev
index.dev › blog › nan-values-in-python
4 Easy Ways to Check for NaN Values in Python (+Examples)
The most straightforward way to check for NaN values is by using the np.isnan() function from the NumPy library.
🌐
Readthedocs
stable-baselines.readthedocs.io › en › master › guide › checking_nan.html
Dealing with NaNs and infs — Stable Baselines 2.10.3a0 documentation
import numpy as np np.seterr(all='raise') # define before your code. print("numpy propagation test:") a = np.float64('NaN') b = np.float64(1.0) val = a + b # this will neither warn nor raise anything print(val) Tensorflow can add checks for detecting and dealing with invalid value: tf.add_check_numerics_ops and tf.check_numerics, however they will add operations to the Tensorflow graph and raise the computation time.
🌐
Appdividend
appdividend.com › np-nan
Numpy.nan (np.nan) in Python
July 11, 2025 - import numpy as np nan_value = np.nan print(nan_value) # Output: nan print(f"Type: {type(nan_value)}") # Output: Type: <class 'float'> print(f"Data type: {np.dtype(type(nan_value))}") # Output: Data type: float64 · Let’s create an array that contains a NaN value.
🌐
iCert Global
icertglobal.com › home › community › what is the best way to check for nan (not a number) values in python?
How to Check for NaN in Python NumPy and Pandas | iCertGlobal Community
August 13, 2025 - The reason the equality operator fails is that, by definition, NaN != NaN. To check a single float value, use math.isnan(x). If you are working with large datasets, numpy.isnan(array) is essential as it is vectorized and extremely fast.
🌐
Readthedocs
datatest.readthedocs.io › en › stable › how-to › nan-values.html
How to Deal With NaN Values — datatest 0.11.1 documentation
>>> import decimal, math, numpy ... == Invalid(numpy.float64('nan')) True · Some packages provide a NaN constant that can be referenced in user code (e.g., math.nan and numpy.nan). While it may be tempting to use these constants to check for matching NaN values, this ...
🌐
Readthedocs
datatest.readthedocs.io › en › latest › how-to › nan-values.html
How to Deal With NaN Values — datatest 0.12.0.dev1 documentation
>>> import decimal, math, numpy ... == Invalid(numpy.float64('nan')) True · Some packages provide a NaN constant that can be referenced in user code (e.g., math.nan and numpy.nan). While it may be tempting to use these constants to check for matching NaN values, this ...
🌐
TutorialsPoint
tutorialspoint.com › test-array-values-for-nan-in-numpy
Test array values for NaN in Numpy
February 8, 2022 - import numpy as np # Create an ... print("\nNumber of elements...<br>", arr.size) # To test array for NaN, use the numpy.isnan() method in Python Numpy print("\nTest array for NaN...<br>",np.isnan(arr)) Array......
🌐
HumanKode
humankode.com › python › check-for-nan-values-in-python
Check for NaN values in Python - HumanKode
June 15, 2025 - Checking for NaN (Not a Number) values in Python can be done through several methods, depending on your specific needs and the libraries you're using.
🌐
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?