将日期列和时间列合并到日期时间

问题描述 投票:0回答:1

我有两列(两个文本对象),一个日期,另一个小时结束。

df = pd.DataFrame({'Date' : ['2018-10-01', '2018-10-01', '2018-10-01'],
                'Hour_Ending': ['1.0', '2.0', '3.0']})

如何将两列一起添加以获得看起来像这样的datetime对象?

2018-10-01 01:00

作为奖励,如何将Hour_Ending更改为Hour_Starting?

pandas datetime
1个回答
1
投票

使用to_datetimeTimedelta

pd.to_datetime(df.Date)+pd.to_timedelta(df.Hour_Ending.astype('float'), unit='h')
Out[122]: 
0   2018-10-01 01:00:00
1   2018-10-01 02:00:00
2   2018-10-01 03:00:00
dtype: datetime64[ns]
© www.soinside.com 2019 - 2024. All rights reserved.