简体   繁体   中英

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'

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

Try

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM