简体   繁体   English

熊猫将小时索引整数转换为日期时间

[英]pandas convert Hour index integer to datetime

i have a Pandas dataframe like this:我有一个像这样的 Pandas 数据框:

   Date    Hour Actual      
2018-06-01  0   0.000000
2018-06-01  1   0.012000
2018-06-01  2   0.065000
2018-06-01  3   0.560000
...

I want to convert these Hour integer indexes and add to date so that it is a Pandas' datetime object.我想转换这些 Hour 整数索引并添加到​​日期,以便它是 Pandas 的 datetime 对象。 The result should be like this:结果应该是这样的:

       Date            Actual       
2018-06-01 00:00:00   0.000000
2018-06-01 01:00:00   0.012000
2018-06-01 02:00:00   0.065000
2018-06-01 03:00:00   0.560000
...

What would be an efficient way to do that?这样做的有效方法是什么? Does Panda provide functionality for converting integer indexes into datetime objects? Panda 是否提供将整数索引转换为日期时间对象的功能?

Use to_datetime with to_timedelta and pop for extract column time :使用to_datetimeto_timedeltapop提取列time

df['Date'] = pd.to_datetime(df['Date']) + pd.to_timedelta(df.pop('Hour'), unit='H')
print (df)
                 Date  Actual
0 2018-06-01 00:00:00   0.000
1 2018-06-01 01:00:00   0.012
2 2018-06-01 02:00:00   0.065
3 2018-06-01 03:00:00   0.560

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

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