Sometimes, we want to check for NaN values with Python.
In this article, we’ll look at how to check for NaN values with Python.
How to check for NaN values with Python?
To check for NaN values with Python, we can use the math.isnan method.
For instance, we write:
import math
x = float('nan')
isnan = math.isnan(x)
print(isnan)
We call float with a non-number string and assign that to x.
Then we call math.isnan with x to check if x is NaN or not and assign the returned result to isnan.
Therefore, isnan is True since x is not a number.
Conclusion
To check for NaN values with Python, we can use the math.isnan method.