Get strings with empty pandas python dates

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 empty Dates lines and save it in 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 row, this piece of code works, it also works with numbers that I count

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

but how can I compare with empty date strings?

+4
source share
3 answers

One way is to replace empty cells with nan, and then use isnull ()

df.Dates = df.Dates.replace('', np.nan)
blankDate = df[df.Dates.isnull()]
+3
source
#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
+3
source

Forms are allowed Falsewhen created as a Boolean

df[~df.Dates.astype(bool)]
+2
source

Source: https://habr.com/ru/post/1677722/


All Articles