Home > Back-end >  DateTime column in the DataFrame
DateTime column in the DataFrame

Time:10-12

Interventions_df['From DateTime'] = pd.to_datetime(Interventions_df['From DateTime'], dayfirst = True)
Interventions_df['To DateTime'] = pd.to_datetime(Interventions_df['To DateTime'], dayfirst = True)
ParserError: Unknown string format: 14-09-2021  10:39:00:00

CodePudding user response:

If you would like to get rid of this error, you can try to pass the format argument in pd.to_datetime(), so the parser can recognize the str format you are giving. Check documentation of this method for more details and syntax details that will help you to formulate this str argument.

CodePudding user response:

The time part is not well formatted 10:39:00:00 (HH:MM:SS:??)

Try:

# Regex to match a correct datetime format
pat = r'(\d{2}-\d{2}-\d{4}\s \d{2}:\d{2}:\d{2})

#
# I rename 'Inteventions_df' to 'df' for readability
#

df['From DateTime'] = \
    df['From DateTime'].str.extract(pat, expand=False).astype('datetime64')

df['To DateTime'] = \
    df['To DateTime'].str.extract(pat, expand=False).astype('datetime64')
  • Related