在 Pandas 中将每小时数据上采样到 5 分钟数据 [英] Upsampling hourly data to 5 minute data in pandas
本文介绍了在 Pandas 中将每小时数据上采样到 5 分钟数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据:
MTU (CET) Day-ahead Price [EUR/MWh]
0 09.10.2017 00:00 - 09.10.2017 01:00 43.13
1 09.10.2017 01:00 - 09.10.2017 02:00 34.80
2 09.10.2017 02:00 - 09.10.2017 03:00 33.31
3 09.10.2017 03:00 - 09.10.2017 04:00 32.24
.......
22 09.10.2017 22:00 - 09.10.2017 23:00 49.06
23 09.10.2017 23:00 - 10.10.2017 00:00 38.46
我希望每 5 分钟获取一次数据.通过使用:
From which I would like to have data for every 5 minutes. By using:
price = pd.read_csv(price_data)
price_x = price.set_index(pd.DatetimeIndex(price['MTU (CET)'].str[:-19]))
price2 = price_x.resample('300S').pad()
我得到以下数据:
2017-09-10 00:00:00 43.13
2017-09-10 00:05:00 43.13
2017-09-10 00:10:00 43.13
...
2017-09-10 22:45:00 49.06
2017-09-10 22:50:00 49.06
2017-09-10 22:55:00 49.06
2017-09-10 23:00:00 38.46
但是,对于 23:00 到 00:00 之间的分钟,价格也应该是 38.46.有人知道怎么帮忙吗?
However, for the minutes between 23:00 and 00:00 the price should also be 38.46. Does anyone know how to help?
推荐答案
您需要手动添加下一个小时的最后一行以及 iloc
选择的最后一行的数据:
You need manually add last row with next hour and with data from last row seelcted by iloc
:
price_x = price.set_index(pd.DatetimeIndex(price['MTU (CET)'].str[:-19]))
price_x.loc[price_x.index[-1] + pd.Timedelta(1, unit='h')] = price_x.iloc[-1]
print (price_x.tail(3))
Day-ahead Price [EUR/MWh]
MTU (CET)
2017-09-10 22:00:00 49.06
2017-09-10 23:00:00 38.46
2017-09-11 00:00:00 38.46
<小时>
price2 = price_x.resample('300S').pad()
print (price2.tail(20))
Day-ahead Price [EUR/MWh]
MTU (CET)
2017-09-10 22:25:00 49.06
2017-09-10 22:30:00 49.06
2017-09-10 22:35:00 49.06
2017-09-10 22:40:00 49.06
2017-09-10 22:45:00 49.06
2017-09-10 22:50:00 49.06
2017-09-10 22:55:00 49.06
2017-09-10 23:00:00 38.46
2017-09-10 23:05:00 38.46
2017-09-10 23:10:00 38.46
2017-09-10 23:15:00 38.46
2017-09-10 23:20:00 38.46
2017-09-10 23:25:00 38.46
2017-09-10 23:30:00 38.46
2017-09-10 23:35:00 38.46
2017-09-10 23:40:00 38.46
2017-09-10 23:45:00 38.46
2017-09-10 23:50:00 38.46
2017-09-10 23:55:00 38.46
2017-09-11 00:00:00 38.46
这篇关于在 Pandas 中将每小时数据上采样到 5 分钟数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文