Pandas To_Datetime Transformation, Format with Dots

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"

1 Answer

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest