pandas将小时索引整数转换为datetim

2024-04-24 14:26:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个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
...

我想转换这些小时整数索引并将其添加到date,使其成为Pandas的datetime对象。 结果应该是这样的:

^{pr2}$

这样做有效率吗?Panda是否提供将整数索引转换为datetime对象的功能?在


Tags: 数据对象功能pandasdatetimedate整数panda
1条回答
网友
1楼 · 发布于 2024-04-24 14:26:41

^{}^{}和{a3}一起用于提取柱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

相关问题 更多 >