Issue
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
Solution
Why not just wrap whatever you pass to math.isnan
with another float
conversion? If it was already a float (i.e. float('nan')
) you just made a "redundant" call:
import math
def is_nan(value):
return math.isnan(float(value))
And this seems to give your expected results:
>>> is_nan(float('nan'))
True
>>> is_nan('nan')
True
>>> is_nan(np.nan)
True
>>> is_nan(5)
False
>>> is_nan('5')
False
This will still raise a ValueError
for non-numeric (except 'nan'
) strings. If that's a problem, you can wrap with try/except
. As long as the float
conversion worked, there is no reason for isnan
to fail. So we are basically catching non-numeric strings that my fail the float
conversion:
def is_nan(value):
try:
return math.isnan(float(value))
except ValueError:
return False
Any non-numeric string is surely not a NaN value so return False
.
Answered By - Tomerikoo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.