My numpy arrays use np.nan
to designate missing values. As I iterate over the data set, I need to detect such missing values and handle them in special ways.
Naively I used numpy.isnan(val)
, which works well unless val
isn't among the subset of types supported by numpy.isnan()
. For example, missing data can occur in string fields, in which case I get:
>>> np.isnan('some_string')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Not implemented for this type
Other than writing an expensive wrapper that catches the exception and returns False
, is there a way to handle this elegantly and efficiently?
I found this brilliant solution here, it uses the simple logic NAN!=NAN. https://www.codespeedy.com/check-if-a-given-string-is-nan-in-python/
Using above example you can simply do the following. This should work on different type of objects as it simply utilize the fact that NAN is not equal to NAN.
import numpy as np
s = pd.Series(['apple', np.nan, 'banana'])
s.apply(lambda x: x!=x)
out[252]
0 False
1 True
2 False
dtype: bool
Is your type really arbitrary? If you know it is just going to be a int float or string you could just do
if val.dtype == float and np.isnan(val):
assuming it is wrapped in numpy , it will always have a dtype and only float and complex can be NaN
Source: Stackoverflow.com