Pandas 在python中合并时间戳中的字符串日期和整数小时
我试图用Prophet预测未来的价值。预测相当准确,但数据预处理部分需要相当长的时间,因为我不知道如何处理以下问题: 鉴于各栏:Pandas 在python中合并时间戳中的字符串日期和整数小时,pandas,time-series,Pandas,Time Series,我试图用Prophet预测未来的价值。预测相当准确,但数据预处理部分需要相当长的时间,因为我不知道如何处理以下问题: 鉴于各栏: Date - of type object in the same format as the example: 2019-01-01 00:00:00 ( So note that they all have 00:00:00 even though the hour associated to the specific row may
Date - of type object in the same format as the example: 2019-01-01
00:00:00 ( So note that they all have 00:00:00 even though the hour
associated to the specific row may not be 0)
Hour - of type int in the range [0,23]
我想创建一个名为let's say Time的新列,其中包含日期和关联的小时。所以如果我和你吵架
日期=2019-01-01 00:00:00,小时=13
我希望相应的时间列包含值2019-01-01 13:00:00。然后我会删除日期和小时列,因为我不再需要它们了
我使用下面的代码成功地做到了这一点,但效率非常低,因为对于2000个条目,它需要大约2分钟,而我的整个数据集实际上包含数百万个实例
你能推荐一个更好的替代方法吗
非常感谢你的帮助
data = df_all.loc[df_all['Cell_Id']==top_cells[0]]
data['Time'] = 0
for i in range(len(data)):
data['Time'].iloc[i] = pd.to_datetime(str(data['Date'].iloc[i])[:10].replace('-',"")+str(data['Hour'].iloc[i]), format = '%Y%m%d%H')
您可以对提取列使用和:
data = pd.DataFrame({'Date':['2019-01-01','2019-01-02'],
'Hour':[4,5]})
data['Datetime'] = (pd.to_datetime(data.pop('Date')) +
pd.to_timedelta(data.pop('Hour'), unit='h'))
print (data)
Datetime
0 2019-01-01 04:00:00
1 2019-01-02 05:00:00