Use math.isnan:

>>> import math
>>> x = float('nan')
>>> math.isnan(x)
True
Answer from gimel 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 Python, the float type includes nan, which can be created using float('nan'). Other creation methods will be described later. print(float('nan')) # nan print(type(float('nan'))) # <class 'float'> ... For example, when reading a CSV file with ...
Discussions

python - How to check for NaN values - Stack Overflow
That is, float('nan') == float('nan') returns False — which is a strange convention, but basically part of the definition of a NaN. The approach you want is actually the one posted by Chris Jester-Young, below. 2019-07-11T15:38:20.063Z+00:00 ... C. K. Young · 225k4747 gold badges395395 silver badges447447 bronze badges ... Word of warning: quoting Bear's comment below "For people stuck with python ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to set a number to NaN or infinity? - Stack Overflow
The float constructor is actually case-insensitive, so you can also use float("NaN") or float("InFiNiTy"). The cmath and numpy constants return plain Python float objects. More on stackoverflow.com
🌐 stackoverflow.com
python function or operation that returns float("nan") - Stack Overflow
the IEEE Standard for Floating-Point Arithmetic (IEEE 754) requires the existence of a float (or two...) that is called nan (not a number). there are two ways to get nan (that i know of) nan = float(& More on stackoverflow.com
🌐 stackoverflow.com
math.isnan() vs = float('nan')
That's very interesting, because what you describe is the opposite of the expected behavior. >>> float('nan') == float('nan') False >>> math.isnan(float('nan')) True Show your code, please use a code block if posting on reddit. https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F Or paste in pastebin.com and give a link here. More on reddit.com
🌐 r/learnpython
1
2
November 24, 2022
🌐
Educative
educative.io › answers › how-to-assign-nan-to-a-variable-in-python
How to assign NaN to a variable in Python
Note that the “NaN” passed to the float is not case sensitive. All of the four variables come out as NaN. ... We can also use Python’s decimal library instead of floats. For example, we can use the Decimal("Nan") method instead of float("Nan").
🌐
Turing
turing.com › kb › nan-values-in-python
How to Check for NaN Values in Python?[With Examples]
NaN values are considered unequal to all other values, including themselves. Consider the following example: value = float('nan') if value != value: print("Value is NaN") else: print("Value is not NaN")
🌐
CodeRivers
coderivers.org › blog › python-float-nan
Demystifying Python Float NaN: Concepts, Usage, and Best Practices - CodeRivers
February 22, 2026 - For example: nan_value = float('nan') result1 = nan_value + 5 result2 = nan_value * 2 print(result1) print(result2) This behavior can be a source of bugs if not properly accounted for, especially when working with loops or complex calculations. In Python lists, you can store NaN values just ...
🌐
W3Schools
w3schools.com › python › ref_math_nan.asp
Python math.nan Constant
The nan constant is equivalent to float('nan'). ... Coding fundamentals as bite-sized lessons and challenges. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Find elsewhere
🌐
Finxter
blog.finxter.com › 5-effective-ways-to-set-float-to-nan-in-python
5 Effective Ways to Set Float to NaN in Python – Be on the Right Side of Change
This snippet creates a new floating-point variable my_float and sets its value to ‘NaN’. The float('nan') call converts the string ‘nan’ to an actual NaN value. Python’s math module provides a convenient nan attribute that represents the NaN value.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › numpy tutorial › numpy nan
NumPy NaN | Working of NumPy NaN in Python with Examples
April 19, 2023 - In NumPy uses nan to assign variables which do not have any values and are needed to be computed. But we should note that in Python NaN is not similar to infinity and we can create NaN values also using float and numpy.nan. Below are the examples of how to create or initialize the array with ...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Built In
builtin.com › data-science › how-to-use-make-float-in-python
How to Use Float in Python (With Sample Code!) | Built In
The name is an acronym for “not a number.” In Python, nan is a specific kind of float value. It cannot be converted to a different value and behaves unlike typical numerical values, but it’s worth being aware that it’s technically a float. Correspondingly, you can actually make a nan with the float() function. You do it by passing a string as we did above. For example, the following code:
🌐
Konfuzio
konfuzio.com › start › blog › python › nan python - how to handle nan values in python correctly
NaN Python - How to handle NaN values in Python correctly
May 24, 2025 - For example there are 5 + float('nan') float('nan') back. NaN is a numeric value used specifically in mathematical and numerical contexts, while None more commonly used in Python to represent the absence of a value.
🌐
Towards Data Science
towardsdatascience.com › home › data science › nan values in the python standard library
NaN Values in the Python Standard Library | Towards Data Science
October 7, 2023 - While it's not the best tool to work with directly, it enables you to work with Cython efficiently without using non-standard-library tools like NumPy. An example of a dedicated numerical tool from the standard library is the math module: math - Mathematical functions - Python 3.10.8 documentation · You have two ways to use NaN values in the Python standard library: float("nan") and math.nan.
🌐
GeeksforGeeks
geeksforgeeks.org › python › float-in-python
float() in Python - GeeksforGeeks
September 18, 2025 - A floating-point number is simply a number with a decimal point (for example, 3.14, -0.5, or 10.0). This function is especially useful when dealing with user input or when precise decimal calculations are needed.
🌐
Better Programming
betterprogramming.pub › did-you-know-float-nan-and-float-inf-exist-in-python-5a3b1054f4ce
Did You Know Float(“NaN”) and Float(“inf”) Exist in Python? | by Görkem Arslan | Better Programming
February 2, 2022 - We can define the negative infinity as -inf and the positive infinity as inf in Python. In mathematics, sqrt(-1) in real numbers or 0/0 (zero over zero) gives an undefined number, which can be defined as Nan. In data science, Nan is used to represent the missing values in a dataset. So Nan is basically a placeholder to represent undefined or missing values. You can create the Nan value using float type.
🌐
Index.dev
index.dev › blog › nan-values-in-python
4 Easy Ways to Check for NaN Values in Python (+Examples)
Using the built-in float function: nan_value = float('nan') Using NumPy: import numpy as np nan_value = np.nan · Using the math library (Python 3.5+): import math nan_value = math.nan
🌐
GeeksforGeeks
geeksforgeeks.org › python › difference-between-nan-and-none-in-python
Difference Between Nan and None in Python - GeeksforGeeks
July 23, 2025 - The NaN value in Python could be ... some of the data values. For example, if we want to find out the square root of a negative number let's say -5 the output will be NaN value in NumPy....
🌐
Programiz
programiz.com › python-programming › methods › built-in › float
Python float() | Programiz
March 14, 2024 - In this tutorial, we will learn about the Python float() method with the help of examples.