Home > Blockchain >  remove rows with today's date pandas
remove rows with today's date pandas

Time:08-13

I have a df with several columns, and I want to remove rows that have today's date time.

     col1               col2                  col3                col4
     ABC       2022-08-12 00:03:29.872         123                A1B2
     BCD       2022-08-12 00:02:08.067         234                B1C2
     CDE       2022-08-11 23:57:24.208         345                C1D2   
     DEF       2022-08-11 23:56:55.257         456                D1E2

expected result (assuming today's date is 12th august 2022):

     col1               col2                  col3                col4
     CDE       2022-08-11 23:57:24.208         345                C1D2   
     DEF       2022-08-11 23:56:55.257         456                D1E2

I tried doing below

    df[pd.to_datetime(df.col2, errors='coerce') < pd.to_datetime('today')]

but it is not working, I still get rows from today. Can someone please help me with this?

CodePudding user response:

Use Series.dt.date with != Timestamp.date:

df = df[pd.to_datetime(df.col2, errors='coerce').dt.date != pd.to_datetime('today').date()]
print (df)
  col1                    col2  col3  col4
2  CDE 2022-08-11 23:57:24.208   345  C1D2
3  DEF 2022-08-11 23:56:55.257   456  D1E2
  • Related