Use math.isnan:
>>> import math
>>> x = float('nan')
>>> math.isnan(x)
True
Answer from gimel on Stack OverflowNote.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 ...
Python Course
python-course.eu › numerical-programming › dealing-with-nan.php
33. Dealing with NaN | Numerical Programming | python-course.eu
February 3, 2025 - Python knows NaN values as well. We can create it with "float":
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
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
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
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
02:37
python check if nan value - YouTube
How to Fix the ValueError: cannot convert float NaN to integer ...
01:04
How to fix ValueError: cannot convert float NaN to integer. in ...
04:41
How to turn NaN and floats into integers by using Pandas dataframe ...
ValueError: cannot convert float NaN to integer - YouTube
00:48
How to check for NaN values in Python - YouTube
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").
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
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 ...
Call: +917738666252
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:
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.
Top answer 1 of 5
308
Cast from string using float():
>>> float('NaN')
nan
>>> float('Inf')
inf
>>> -float('Inf')
-inf
>>> float('Inf') == float('Inf')
True
>>> float('Inf') == 1
False
2 of 5
83
Yes, you can use numpy for that.
import numpy as np
a = arange(3,dtype=float)
a[0] = np.nan
a[1] = np.inf
a[2] = -np.inf
a # is now [nan,inf,-inf]
np.isnan(a[0]) # True
np.isinf(a[1]) # True
np.isinf(a[2]) # True
Top answer 1 of 2
4
math.inf - math.inf gives nan.
(1e308 + 1e308 or just 1e309 give inf.)
2 of 2
3
Generating NaN:
math.inf * 0results tonan
(+∞) × 0 = NaN
+(on opposite signs) and/(on any sign) operations betweenmath.infsand bonus case: generating 2
nans:In [576]: divmod(math.inf, 1) Out[576]: (nan, nan)
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.
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.