Jessica
Jessica

Reputation: 3173

get rows with empty dates pandas python

it looks like this:

      Dates  N-D  unit
0  1/1/2016  Q1   UD
1            Q2   UD
2            Q3   UD
3  2/1/2016  Q4   UD
4  5/1/2016  Q5   UD
5            Q6   UD

I want to filter out the empty Dates rows and save it in a dataframe blankDate:

      Dates  N-D  unit
1            Q2   UD
2            Q3   UD
5            Q6   UD


 blankDate=df1[df1['Dates']== '']  #this didn't work 
 df1['Discharge Date'] = pd.to_datetime(df1['Discharge Date']) #then I converted the column to date format but still doesn't work

if the column is a string this piece of code works, it also works on numbers I think

blankDate=df1[df1['stringcolumn']== '']

but how do I compare to empty date rows?

Upvotes: 7

Views: 12385

Answers (3)

piRSquared
piRSquared

Reputation: 294508

Blanks resolve to False when cast as Boolean

df[~df.Dates.astype(bool)]

Upvotes: 1

Allen Qin
Allen Qin

Reputation: 19957

#use pd.isnull to check nans for date type.
df[pd.isnull(pd.to_datetime(df.Dates))]
Out[1512]: 
  Dates N-D unit
1        Q2   UD
2        Q3   UD
5        Q6   UD

Upvotes: 4

Vaishali
Vaishali

Reputation: 38415

One way would be to replace empty cells by nan and then use isnull()

df.Dates = df.Dates.replace('', np.nan)
blankDate = df[df.Dates.isnull()]

Upvotes: 7

Related Questions