Home > OS >  Converting string to datetime [hh:mm:ss]
Converting string to datetime [hh:mm:ss]

Time:09-25

I have two columns Date_x and Date_y. I would like to compare them (i.e Date_x 1 hour < Date_y)

Format of the strings looks as follows "2020-01-29 11:31:32.754292 UTC"

I have tried converting it using datetime:

from datetime import datetime as dt
df["Date_x"] = [dt.strptime(x, '%Y-%m-%d %H:%M:%S.%f') for x in df['Date_x']] 

However, it throws an error regarding the UTC part. I tried removing it with no avail.

Last traceback:

time data '2020-01-29 18:30:28' does not match format '%Y-%m-%d %H:%M:%S.%f'

How would you go about converting the string to hh:mm:ss only?

CodePudding user response:

You could use an if statement:

df["Date_x"] = [dt.strptime(x, '%Y-%m-%d %H:%M:%S.%f') if '.' in x else dt.strptime(x, '%Y-%m-%d %H:%M:%S') for x in df['Date_x']] 

But why not just pd.to_datetime:

df["Date_x"] = pd.to_datetime(df["Date_x"], infer_datetime_format=True)
  • Related