【问题标题】:aggregate and distribute time series data聚合和分发时间序列数据
【发布时间】:2021-12-20 10:00:26
【问题描述】:

我在 pandas 数据框中有一些时间序列数据,如下所示:

begin end mw_values
2021-09-14 11:16:00 2021-09-14 11:27:11 0
2021-09-14 11:27:11 2021-09-14 11:30:00 100
2021-09-14 11:30:00 2021-09-14 11:33:59 1200
2021-09-14 11:33:59 2021-09-14 11:39:42 600
2021-09-14 11:39:42 2021-09-14 11:59:59 400

我需要将 mw_values 的总和分配到 15 分钟的时间段中,如下所示:

time_slots_15_min sum_mw_values
2021-09-14 11:00 0
2021-09-14 11:15 100
2021-09-14 11:30 2200
2021-09-14 11:45 0
2021-09-14 12:00 0

有人知道我如何实现这一目标吗?

请注意,开始和结束之间的间隔可能会重叠 2 个时隙。那么该值必须包含在它开始的时隙的总和中;例如上面示例中的 mw_value 为 400。

【问题讨论】:

  • “最终”值真的相关吗?
  • @Riley 看起来只有开始值很重要。两个答案都按"begin" 列重新索引,然后重新采样

标签: python pandas time-series rolling-computation pandas-resample


【解决方案1】:

您可以对数据框重新采样,以便将数据汇总到 15 分钟的 bin 中。然后您可以重新索引该帧,使其与您想要的开始/结束/频率时间相匹配。

freq = "15min"
new_index = pd.date_range(
    "2021-09-14 11:00:00", "2021-09-14 12:00:00", freq=freq
)

out = (
    df.resample(freq, on="begin")["mw_values"]
    .sum()
    .reindex(new_index, fill_value=0)
    .to_frame("sum_mw_values")
)

print(out)
                     sum_mw_values
2021-09-14 11:00:00              0
2021-09-14 11:15:00            100
2021-09-14 11:30:00           2200
2021-09-14 11:45:00              0
2021-09-14 12:00:00              0

【讨论】:

  • 嗨,卡梅伦,非常感谢。您的解决方案解决了我的问题。
【解决方案2】:

您可以通过begin 列重新索引您的DataFrame,插入两个新行以确保开始时间从11:00 开始并且结束时间是12:00),然后使用.resample("15min").sum() 这将为DatetimeIndex 工作(如果您想进一步阅读,可以在here 找到文档):

## in case your column isn't already a datetime
df["begin"] = pd.to_datetime(df["begin"])

df = df.set_index("begin")

## add beginning and ending times to df
df_start_end = pd.DataFrame({"end": ["2021-09-14 11:15:00","2021-09-14 12:15:00"], "mw_values":[0]}, index=[pd.to_datetime("2021-09-14 11:00:00"),pd.to_datetime("2021-09-14 12:00:00")])
df_final = pd.concat([df_start_end,df]).sort_index()

这是df_final 的样子:

                                     end  mw_values
2021-09-14 11:00:00  2021-09-14 11:15:00          0
2021-09-14 11:16:00  2021-09-14 11:27:11          0
2021-09-14 11:27:11  2021-09-14 11:30:00        100
2021-09-14 11:30:00  2021-09-14 11:33:59       1200
2021-09-14 11:33:59  2021-09-14 11:39:42        600
2021-09-14 11:39:42  2021-09-14 11:59:59        400
2021-09-14 12:00:00  2021-09-14 12:15:00          0

然后我们每 15 分钟对 DatetimeIndex 进行重新采样和求和:

## sum by every 15 minutes from the start to end time
df_final.resample("15min").sum().reset_index().rename(columns={"index":"time_slots_15_min","mw_values":"sum_mw_values"})

输出:

    time_slots_15_min  sum_mw_values
0 2021-09-14 11:00:00              0
1 2021-09-14 11:15:00            100
2 2021-09-14 11:30:00           2200
3 2021-09-14 11:45:00              0
4 2021-09-14 12:00:00              0

【讨论】:

  • 嗨,德里克,感谢您的帮助。我使用了 Cameron 的解决方案,因为它更短。但是,您对 cme​​ts 的类似但更详细的方法帮助我了解了重采样在 pandas 中的工作原理。
  • @ThomasKutsch 不用担心,我很高兴能帮上忙!
猜你喜欢
  • 1970-01-01
  • 2021-07-18
  • 2016-06-14
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 2015-10-03
  • 1970-01-01
相关资源
最近更新 更多