Use math.isnan:

>>> import math
>>> x = float('nan')
>>> math.isnan(x)
True
Answer from gimel on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_math_isnan.asp
Python math.isnan() Method
The math.isnan() method checks whether a value is NaN (Not a Number), or not.
๐ŸŒ
Turing
turing.com โ€บ kb โ€บ nan-values-in-python
How to Check for NaN Values in Python?[With Examples]
The math module in Python provides the isnan() function, which can be used to check if a value is NaN. This method works only with floating-point values.
๐ŸŒ
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.
๐ŸŒ
Digis
digiscorp.com โ€บ python-check-if-value-is-nan-a-complete-overview
Python Check If Value Is NaN: A Complete Overview
September 7, 2026 - For an individual numeric value in standard Python code, math.isnan() is usually the clearest option.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-isnan
Python isnan
August 10, 2026 - The Python isnan function is one of the Mathematical functions used to check whether the given value is a valid number or not.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.isnan.html
numpy.isnan โ€” NumPy v2.1 Manual
>>> 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])
๐ŸŒ
DataCamp
datacamp.com โ€บ home โ€บ tutorials โ€บ python โ€บ python nan: 4 ways to check for missing values in python
Python NaN: 4 Ways to Check for Missing Values in Python | DataCamp
February 15, 2024 - When dealing with missing values in Python, the approach largely depends on the data structure you're working with. NumPy's isnan() function is ideal for identifying NaNs in numeric arrays or single values, offering a straightforward and efficient solution.
Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ math module โ€บ math.isnan()
Python | Math Module | math.isnan() | Codecademy
September 4, 2024 - In Python, the math.isnan() function returns True if the given value is NaN (not a number) and False if the value is a number.
๐ŸŒ
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 - Convert between bool (True/False) and other types in Python ยท As you can see with bool(), nan is evaluated as True. ... Use math.isnan() or np.isnan().
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-math-library-isnan-method
Python math library | isnan() method - GeeksforGeeks
February 20, 2023 - Syntax : math.isnan(x) Parameters : x [Required] : It is any valid python data type or any number. Returns: Return type is boolean.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-math-library-isnan-method
Python math library | isnan() method | GeeksforGeeks
February 20, 2023 - Syntax : math.isnan(x) Parameters : x [Required] : It is any valid python data type or any number. Returns: Return type is boolean.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-numpyisnan-in-python
What is numpy.isnan() in Python?
Pythonโ€™s numpy.isnan() tests if an element is ยท NaN ยท Not a Number or not. It tests an array element-wise and returns Boolean values.
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ data science โ€บ 5 methods to check for nan values in in python
5 Methods to Check for NaN values in in Python | Towards Data Science
February 23, 2021 - Learn this step by step with the interactive AI and Data Scientist roadmap. isna() in pandas library can be used to check if the value is null/NaN. It will return True if the value is NaN/null.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.isnan.html
numpy.isnan โ€” NumPy v2.5 Manual
numpy.isnan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'isnan'>#
๐ŸŒ
w3resource
w3resource.com โ€บ numpy โ€บ logic-functions โ€บ isnan.php
NumPy Logic functions: isnan() function - w3resource
May 2, 2026 - NumPy Logic functions: isnan() function, example - The isnan() function is used to test element-wise for NaN and return result as a boolean array.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ check for nan values in python
Check for NaN Values in Python Using math.isnan | Sentry
October 2, 2026 - Use the math.isnan function to check whether a float value is NaN in Python, with a robust implementation that handles non-numeric types using try-except
๐ŸŒ
Index.dev
index.dev โ€บ blog โ€บ check-for-nan-values-in-python
4 Easy Ways to Check for NaN Values in Python (+Examples)
The np.isnan() function returns a boolean array indicating which elements in the input array are NaN.