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

Viewed 15840

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'
4 Answers

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(", ")]

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')

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)

Try

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