In some data I am processing I am encountering data of the type float
, which are filled with 'nan', i.e. float('nan')
.
However checking for it does not work as expected:
float('nan') == float('nan')
>> False
You can check it with math.isnan
, but as my data also contains strings (For example: 'nan', but also other user input), it is not that convenient:
import math
math.isnan(float('nan'))
>> True
math.isnan('nan')
>> TypeError: must be real number, not str
In the ideal world I would like to check if a value is in a list of all possible NaN values, for example:
import numpy as np
if x in ['nan', np.nan, ... ]:
# Do something
pass
Now the question:
How can I still use this approach but also check for float('nan')
values? And why equals float('nan') == float('nan')
False