获取空日期pandas python的行

时间:2017-05-22 23:24:40

标签: python pandas

它看起来像这样:

      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

我想过滤掉空的Dates行并将其保存在数据帧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

如果列是一个字符串,这段代码可以工作,它也适用于我认为的数字

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

但是如何与空日期行进行比较?

3 个答案:

答案 0 :(得分:4)

一种方法是用nan替换空单元格,然后使用isnull()

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

答案 1 :(得分:3)

#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

答案 2 :(得分:2)

当转换为布尔值

时,空白解析为False
df[~df.Dates.astype(bool)]