float('nan')
represents NaN (not a number). But how do I check for it?
Use math.isnan
:
>>> import math
>>> x = float('nan')
>>> math.isnan(x)
True
The usual way to test for a NaN is to see if it's equal to itself:
def isNaN(num):
return num != num
numpy.isnan(number)
tells you if it's NaN
or not.
Here are three ways where you can test a variable is "NaN" or not.
import pandas as pd
import numpy as np
import math
# For single variable all three libraries return single boolean
x1 = float("nan")
print(f"It's pd.isna: {pd.isna(x1)}")
print(f"It's np.isnan: {np.isnan(x1)}}")
print(f"It's math.isnan: {math.isnan(x1)}}")
Output
It's pd.isna: True
It's np.isnan: True
It's math.isnan: True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With