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?
pandashaspandas.isnull(): I'm not sure if that meets your needs, so some example data might be good. - Mariuspandas.isnull()seems to work perfectly. The only data type I'm currently dealing with which breaksnumpy.isnan()is string, andpandas.isnull()handles it well. In fact, it seems to handle well all any arbitrary object I threw at it. Were there any specific issues you were concerned about? Otherwise, you may want to submit your comment as a full-fledged answer, since it seems like the canonical answer, at least for pandas users. - Dun Peal