简体   繁体   English

Pandas to_datetime 转换,带点的格式

[英]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.为此,我正在使用 pandas 重采样方法。 Beforehand I need to convert the data type of the column containg the timestamp from object to datetime.事先我需要将包含时间戳的列的数据类型从 object 转换为日期时间。

I'm using the pandas to_datetime()-Method for that with the following line of code:我正在使用 pandas to_datetime()-Method 和以下代码行:

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) "在这里,我总是收到错误消息:“时间数据‘03.11.2020 15:34’与格式‘%d.%m.%y %H:%M’不匹配(匹配)

I don't really understand why I get this error since the timestamp has the format "day.month.year hour:minute"我真的不明白为什么会出现此错误,因为时间戳的格式为“day.month.year hour:minute”

Use Y (with a capital) for the YYYY format with century, see strptime documentation:使用Y (带大写字母)表示带有世纪的 YYYY 格式,请参阅strptime文档:

Directive指示 Meaning意义 Example例子
%y %y Year without century as a zero-padded decimal number.没有世纪的年份作为零填充的十进制数。 00, 01, …, 99 00, 01, …, 99
%Y %Y Year with century as a decimal number.以世纪为十进制数的年份。 0001, 0002, …, 2013, 2014, …, 9998, 9999 0001, 0002, …, 2013, 2014, …, 9998, 9999
df_resampled['DateTime'] = pd.to_datetime(df_resampled['DateTime'],
                                          format="%d.%m.%Y %H:%M")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM