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

11,306

Solution 1

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

Solution 2

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')
Share:
11,306
NotRikBurgers
Author by

NotRikBurgers

Updated on June 07, 2022

Comments

  • NotRikBurgers
    NotRikBurgers almost 2 years

    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'
    
  • NotRikBurgers
    NotRikBurgers almost 4 years
    It works for when there is only one date, but when there are more than one I get: 'unconverted data remains: "dates after first" '