我有不规则间隔的时间序列数据。我有总的能量消耗和能量使用的持续时间。在
Start Date Start Time Duration (Hours) Usage(kWh)
1/3/2016 12:28:00 PM 2.233333333 6.23
1/3/2016 4:55:00 PM 1.9 11.45
1/4/2016 6:47:00 PM 7.216666667 11.93
1/4/2016 7:00:00 AM 3.45 9.45
1/4/2016 7:26:00 AM 1.6 7.33
1/4/2016 7:32:00 AM 1.6 4.54
我想计算15分钟内所有负荷曲线的总和。我可以在必要时取整(例如,最近的1分钟)。我不能立即使用重采样,因为它会将使用量平均到下一个时间戳中,在第一次输入1/3 12:28 PM的情况下,需要6.23 kWH并将其均匀分布到下午4:55,这是不准确的。6.23 kWh应持续到12:28 PM+2.23小时~=2:42 PM。在
这是一个直接的实现,它只是建立一个系列,
result
,其索引具有分钟频率,然后在df
(使用df.itertuples
)并为每个添加适当的功率 相关间隔中的行:关于性能的注意事项:循环遍历
^{pr2}$df
的行不是很好 特别是当len(df)
很大时。为了获得更好的性能,您可能需要 more clever method,它处理 以矢量化方式“一次”所有行:当
len(df)
等于1000,using_cumsum
比using_loop
快10倍以上:下面我使用的解决方案是itertuples方法。请注意使用numpy.sum函数对我不起作用。我改为使用pandas重采样关键字“how”并将其设置为sum。在
我还重命名了文件中的列,使导入更容易。在
我不受时间/资源限制,所以我使用itertuples方法,因为它很容易实现。在
i启动代码
相关问题 更多 >
编程相关推荐