How to Check for NaN Values in Python
Created
Modified
Using math.isnan Method
The math.isnan(x)
method returns True if x is a NaN (not a number), and False otherwise. For example,
#!/usr/bin/python3
# Import module
import math
f = float('nan')
print(math.isnan(f))
True
Using NaN Implement
A NaN implemented following the standard, is the only value for which the inequality comparison with itself should return True:
#!/usr/bin/python3
def isNaN(n):
return n != n
f = float('nan')
print(isNaN(f))
True