python - 在 Pandas 中将每小时数据上采样为 5 分钟数据

标签 python pandas sampling

我有以下数据:

                              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 分钟从中获取一次数据。 通过使用:

    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。有谁知道如何提供帮助?

最佳答案

您需要手动添加下一个小时的最后一行以及 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  

关于python - 在 Pandas 中将每小时数据上采样为 5 分钟数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46666464/

相关文章:

python - Python 是线程安全的吗?

python - 向 Pandas DataFrame 添加额外索引

python - Pandas :用 0 替换非数字单元格

android - 定制的android dumpsys meminfo

r - 洗牌向量 - 样本()的所有可能结果?

Python SSL证书根据通用名称检查主机名

python - 使用 pandas 根据条件替换值

python - 在Python中将字符串数据转换为整数

python - 将数量复制为新列

r - 在r中生成大量样本