Home > Back-end >  Pandas to_datetime transformation, format with dots
Pandas to_datetime transformation, format with dots

Time:03-15

I want to aggregate rows with time data which were measured every minute to rows summing up the data for hours/days. I'm using the pandas resample method for that. Beforehand I need to convert the data type of the column containg the timestamp from object to datetime.

I'm using the pandas to_datetime()-Method for that with the following line of code:

df_resampled['DateTime'] = pd.to_datetime(df_resampled['DateTime'], format="%d.%m.%y %H:%M")

Here I always get the error: "time data '03.11.2020 15:34' does not match format '%d.%m.%y %H:%M' (match)"

I don't really understand why I get this error since the timestamp has the format "day.month.year hour:minute"

CodePudding user response:

Use Y (with a capital) for the YYYY format with century, see strptime documentation:

Directive Meaning Example
%y Year without century as a zero-padded decimal number. 00, 01, …, 99
%Y Year with century as a decimal number. 0001, 0002, …, 2013, 2014, …, 9998, 9999
df_resampled['DateTime'] = pd.to_datetime(df_resampled['DateTime'],
                                          format="%d.%m.%Y %H:%M")
  • Related