Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Data Science by (17.6k points)

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?

1 Answer

0 votes
by (41.4k points)

pandas.isnull() is used for checking the missing values in numeric and string/object arrays.

Example:

import pandas as pd

import numpy as np

s = pd.Series(['hello’', np.nan, 'world'])

pd.isnull(s)

Output: 

0    False

1     True

2    False

dtype: bool

Here, numpy.nan is used for representing the missing values.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.6k users

Browse Categories

...