ragesz
ragesz

Reputation: 9527

Python pandas .isnull() does not work on NaT in object dtype

I have this series:

ser=pd.Series([11,22,33,np.nan,np.datetime64('nat')],name='my_series')

The series looks like this:

0     11
1     22
2     33
3    NaN
4    NaN
Name: my_series, dtype: object

But I get only one True for NULL values:

ser.isnull()

0    False
1    False
2    False
3     True
4    False
Name: my_series, dtype: bool

Is it a bug or how can I count correctly the NULL values in a pandas series? This does not help:

ser=ser.replace('NaN',np.nan)

Thanks!

Upvotes: 10

Views: 16192

Answers (3)

nck
nck

Reputation: 73

I ran into a similar issue this morning but in an str series! This worked for me and with your sample data as well:

pd.isna(ser)

Upvotes: 6

Yogesh Awdhut Gadade
Yogesh Awdhut Gadade

Reputation: 2708

Surprisingly I got following output after execution the code you given: enter image description here

Alternate way is: count those values which you are able to count.

calculate length of series object and subtraction will give you the count for null values. As follows:**

enter image description here

len(ser)-(ser[ser.isnull()==False]).count()

Upvotes: 1

mirthbottle
mirthbottle

Reputation: 732

To get around this, you can also do

series.apply(lambda x: str(x) == "nat")

Then you can still use np.datetime if you want

Upvotes: 0

Related Questions