Home > other >  Pandas to_datetime() is not formatting AM/PM, and removing zeros
Pandas to_datetime() is not formatting AM/PM, and removing zeros

Time:10-01

I am trying to convert this format: 03-AUG-20 02.55.32.000000000 AM into format='%d-%m-%y %I.%m.%s %p' ie. day-month-year hour(24):min:sec.

but I am getting this error ValueError: 's' is a bad directive in format '%d-%m-%y %I.%m.%s %p'

Also, I intend to remove the zeroes. Unfortunately, I cannot remember the workings of regex anymore.

CodePudding user response:

Change the format time:

>>> df
                          datetime
0  03-AUG-20 02.55.32.000000000 AM

>>> pd.to_datetime(df['datetime'], format=r'%d-%b-%y %I.%M.%S.%f %p')
0   2020-08-03 02:55:32
Name: datetime, dtype: datetime64[ns]
  • Related