Nikhil Jain
Nikhil Jain

Reputation: 79

Unable to filter NaT using isnull()

I have dataframe where one column has significant no. of Nat. I'm using isnull() to filter them but it does not seem to be working. I'm trying to filter all rows where the value in one column is null in to another data frame.

The dtype for the column is datetime.

main['Last Campaign date']

has this output:

0         08-03-2019
1         08-03-2019
2         08-03-2019
3         08-03-2019
4         08-03-2019
5         08-03-2019
6         08-03-2019
7         08-03-2019
8         08-03-2019
9         08-03-2019
10        08-03-2019
11        08-03-2019
12        08-03-2019
13        08-03-2019
14        08-03-2019
15        08-03-2019
16        08-03-2019
17        08-03-2019
18        08-03-2019
19        08-03-2019
20        08-03-2019
21        08-03-2019
22        08-03-2019
23        08-03-2019
24        08-03-2019
25        08-03-2019
26        08-03-2019
27        08-03-2019
28        08-03-2019
29        08-03-2019
             ...    
172801           NaT
172802           NaT
172803           NaT
172804           NaT
172805           NaT
172806           NaT
172807           NaT
172808           NaT
172809           NaT
172810           NaT
172811           NaT
172812           NaT
172813           NaT
172814           NaT
172815           NaT
172816           NaT
172817           NaT
172818           NaT
172819           NaT
172820           NaT
172821           NaT
172822           NaT
172823           NaT
172824           NaT
172825           NaT
172826           NaT
172827           NaT
172828           NaT
172829           NaT
172830           NaT

But when I use the following code, it outputs 0

len(main[main['Last Campaign date'].isnull()]) 

I tried using:

main.replace('NaT', np.nan)
len(main[main['Last Campaign date'] == np.nan])

It is still 0. It seems weird to me and I feel it has something to do with the dtype of column being datetime.

Upvotes: 0

Views: 1417

Answers (2)

zipa
zipa

Reputation: 27879

You can go about it like this:

df[pd.to_datetime(df['Last Campaign date']).isna()]

Please take note that equality assertion using == doesn't yield desired result with np.nan:

np.nan == np.nan
False

Upvotes: 3

Pratyusha Pasumarty
Pratyusha Pasumarty

Reputation: 113

Try main['Last Campaign date'].isna().sum() If you're just trying to count them. Otherwise, if you can replace NaT's with nan's using main.replace(pd.NaT, np.nan)

Upvotes: 0

Related Questions