math.isnan(x) returns True if x is an IEEE 754 Not a Number (nan) and False otherwise.
Because IEEE 754 defines nan == nan as False, you cannot detect NaN using == or in reliably on floats—math.isnan() is the standard, readable test.
math.isnan() is a function in Python's standard math module: import it with import math and call math.isnan(...), or import the name directly with from math import isnan.
Syntax
Import math and pass a numeric value.
python
import math
is_missing = math.isnan(x)
Call
What it does
math.isnan(float("nan"))
Returns True.
math.isnan(math.nan)
Returns True.
math.isnan(float("inf"))
Returns False.
math.isnan(0.0)
Returns False.
Beware Python container quirks: inside a list like [float("nan")], object identity makes nan_obj in lst succeed while float("nan") in lst returns False; always filter with math.isnan().
Function signatures
python
math.isnan(x, /)
# returns: bool True if x is IEEE 754 NaN
Parameters
Parameter
Description
x
Required positional real number (int or float). Strings like "nan" raise TypeError.
Return Value
Return Value
Description
bool
True if x is Not a Number, else False.
Python Examples
Example 1 — See Why x == float("nan") Fails While math.isnan() Works
Contrast broken == equality checks against math.isnan() and verify inf is not NaN.
python
import math
bad = float("nan")
print("bad == bad :", bad == bad)
print("bad != bad :", bad != bad)
print("math.isnan :", math.isnan(bad))
print("isnan(inf) :", math.isnan(math.inf))
Output
bad == bad : False
bad != bad : True
math.isnan : True
isnan(inf) : False
How It Works
Self-equality fails. By IEEE 754 design, NaN compares unequal to everything, including itself.
Explicit predicate is clearer. Writing math.isnan(bad) states intent far better than bad != bad.
Infinities return False. Positive and negative infinity are not NaN.
Example 2 — Clean Missing NaN Readings Before Averaging
Strip NaN markers out of a float dataset so math.fsum() produces a valid average.
python
import math
temps = [21.5, math.nan, 22.0, math.nan, 24.5]
valid = [t for t in temps if not math.isnan(t)]
avg = math.fsum(valid) / len(valid)
print("valid count:", len(valid))
print(f"average: {avg:.2f}")
Output
valid count: 3
average: 22.67
How It Works
NaN poisons totals. Summing the unfiltered list would return nan immediately.
Filter with not isnan. A comprehension keeps the three real temperatures and skips both NaNs.
Clean mean computed. Averaging with math.fsum() gives 22.67.
Example 3 — Watch NaN Propagate Through Arithmetic Without Raising
Observe how arithmetic operations and math functions propagate NaN silently.
python
import math
val = math.nan + 5.0
root = math.sqrt(math.nan)
print(math.isnan(val), math.isnan(root))
print(math.isnan(0.0))
Output
True True
False
How It Works
Contagious across arithmetic. Adding 5.0 to NaN still yields NaN rather than raising.
Functions propagate NaN. Calling math.sqrt(math.nan) returns NaN cleanly instead of throwing ValueError.
Normal zero is False. Ordinary numbers never report NaN.
Frequently Asked Questions
By the IEEE 754 floating-point standard, NaN represents an invalid or unrepresentable result and is never equal to any value, including itself.
No. Strings raise TypeError. Convert with float(s) first before calling math.isnan().
No. Infinities return False for math.isnan() and True for math.isinf().
Yes, operations like float("inf") - float("inf") or math.fsum([math.inf, -math.inf]) produce nan, and math functions given nan return nan.
Use cmath.isnan(z), which returns True if either the real or imaginary part is NaN.