0

I am trying to drop outdated values from a dataframe that holds a difficult to compare date format. My code:

frame=pd.read_csv("somefile.csv")
frame=frame[datetime.strptime(frame["CreationTime"],"%d-%b-%Y %H:%M:%S")>(datetime.now()-timedelta(days=daypar))]

However I get the error that the 1st arg of strptime should be str and not Series, Which makes sense. So how would one go about solving this issue?

MegaFlipFlop
  • 88
  • 10
  • 2
    You can use `pd.to_datetime(frame['CreationTime'], format="%d-%b-%Y %H:%M:%S")` to convert to datetime and drop in a second step. – Dschoni Mar 11 '21 at 12:37
  • 2
    Also see this answer for parsing dates directly on loading: https://stackoverflow.com/questions/17465045/can-pandas-automatically-recognize-dates – Dschoni Mar 11 '21 at 12:39
  • I tried ```pd.to_datetime(frame['CreationTime'], format="%d-%b-%Y %H:%M:%S")``` which errored with " TypeError: Unrecognized value type: " – MegaFlipFlop Mar 11 '21 at 13:03

1 Answers1

1

Pandas read_csv is clever enough to decode timestamps at load time:

frame=pd.read_csv("somefile.csv", parse_dates=['CreationTime'])
frame=frame[df['CreationTime']>(datetime.now()-timedelta(days=daypar))]
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252