amaatouq
amaatouq

Reputation: 2337

Converting string to datetime object in pandas

I have a column called 'SubmitTime' which is a string per observation. An example would be: 'Wed Apr 12 14:42:23 PDT 2017'

I need to sort this dataframe based on submission time (the ones that submitted first, are on top). How can I convert this column into datetime and sort the dataframe in Pandas?

Upvotes: 0

Views: 438

Answers (1)

piRSquared
piRSquared

Reputation: 294218

Assuming you dataframe is df

df.iloc[pd.to_datetime(df.SubmitTime).argsort()]

This leaves your dataframe intact, 'SubmitTime' remains strings

Otherwise, I'd convert 'SubmitTime' to datetime and sort

df.assign(SubmitTime=pd.to_datetime(df.SubmitTime)).sort_values('SubmitTime')

Upvotes: 1

Related Questions