pandas 将小时索引整数转换为日期时间 [英] pandas convert Hour index integer to datetime
本文介绍了 pandas 将小时索引整数转换为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的Pandas数据框:
i have a Pandas dataframe like this:
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
...
我想转换这些Hour整数索引并添加到日期,以便它是Pandas的datetime对象.结果应该是这样的:
I want to convert these Hour integer indexes and add to date so that it is a Pandas' datetime object. 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?
推荐答案
使用 to_timedelta
和 pop
提取列 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
这篇关于 pandas 将小时索引整数转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文