NotRikBurgers
NotRikBurgers

Reputation: 53

time data does not match format '%Y-%m-%d %H:%M:%S'

I got this code but i keep getting this error:

time data "'2019-05-23 11:42:35'" does not match format '%Y-%m-%d %H:%M:%S'

Can someone help me.

df_conv['conversation_raw'].loc[3]
'2019-05-23 11:41:59', '2019-05-23 11:38:57', '2019-05-23 11:31:16'

f = datetime.strptime(df_conv['conversation_raw'].loc[3], '%Y-%m-%d %H:%M:%S')

error: time data "'2019-05-23 11:42:35'" does not match format '%Y-%m-%d %H:%M:%S'

Upvotes: 3

Views: 23449

Answers (4)

LordHammer
LordHammer

Reputation: 83

I used the infer_datetime_format=True so Pandas will automatically choose the right format for you like so:

        dft["Datetime"] = pd.to_datetime(dft["Datetime"],infer_datetime_format=True)

Upvotes: 1

FObersteiner
FObersteiner

Reputation: 25554

Use the built-in to_datetime - it parses the strings correctly, even though there are additional quotes:

import pandas as pd

pd.to_datetime("'2019-05-23 11:42:35'")

Out[1]: Timestamp('2019-05-23 11:42:35')

Upvotes: 1

Gabio
Gabio

Reputation: 9494

Remove the single quotes (') characters from your datetime string ("'2019-05-23 11:42:35'").

Try:

f = datetime.strptime(df_conv['conversation_raw'].loc[0].replace("'",""), '%Y-%m-%d %H:%M:%S')

For multiple datetime strings, try:

f = [datetime.strptime(x.replace("'",""),'%Y-%m-%d %H:%M:%S') for x in df_conv['conversation_raw'].loc[0].split(", ")]

Upvotes: 4

Błotosmętek
Błotosmętek

Reputation: 12927

Try

f = datetime.strptime(df_conv['conversation_raw'].loc[3], "'%Y-%m-%d %H:%M:%S'")

Upvotes: 0

Related Questions