Jase Villam Jase Villam - 1 month ago 14
Python Question

Filtering all rows with NaT in a column in Dataframe python

I have a df like this:

a b c
1 NaT w
2 2014-02-01 g
3 NaT x

df=df[df.b=='2014-02-01']


will give me

a b c
2 2014-02-01 g


I want a database of all rows with NaT in column b?

df=df[df.b==None] #Doesn't work


I want this:

a b c
1 NaT w
3 NaT x

Answer

isnull and notnull work with NaT so you can handle them much the same way you handle NaNs:

>>> df

   a          b  c
0  1        NaT  w
1  2 2014-02-01  g
2  3        NaT  x

>>> df.dtypes

a             int64
b    datetime64[ns]
c            object

just use isnull to select:

df[df.b.isnull()]

   a   b  c
0  1 NaT  w
2  3 NaT  x
Comments