You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Polars计算时间序列中的并发事件数量

计算时间序列中的并发事件数

我有如下时间序列数据:

ts = pl.DataFrame(
    {
        "timestamp": [0, 1, 2, 3, 4, 5],
        "start_index": [0, None, None, 3, None, None],
        "end_index": [4, None, None, 4, None, None],
        }
    )

我需要为每个timestamp索引计算并发事件的数量,具体规则示例:

  • 在timestamp 0处,有一个事件于timestamp 0结束时开始,在timestamp 4结束时终止,覆盖的时间区间为[1, 2, 3, 4]。
  • 在timestamp 3处,另一个事件于timestamp 3结束时开始,在timestamp 4结束时终止,覆盖的时间区间为[4]。

第一个事件与第二个事件在timestamp 3-4期间重叠,因此timestamp 4处的并发事件数为2。无活跃事件的观测值赋值为0。

预期输出如下:

shape: (6, 4)
┌───────────┬─────────────┬───────────┬────────────────────────────┐
│ timestamp ┆ start_index ┆ end_index ┆ number_of_concurrent_event │
│ ---       ┆ ---         ┆ ---       ┆ ---                        │
│ i64       ┆ i64         ┆ i64       ┆ i64                        │
╞═══════════╪═════════════╪═══════════╪════════════════════════════╡
│ 0         ┆ 0           ┆ 4         ┆ 0                          │
│ 1         ┆ null        ┆ null      ┆ 1                          │
│ 2         ┆ null        ┆ null      ┆ 1                          │
│ 3         ┆ 3           ┆ 4         ┆ 1                          │
│ 4         ┆ null        ┆ null      ┆ 2                          │
│ 5         ┆ null        ┆ null      ┆ 0                          │
└───────────┴─────────────┴───────────┴────────────────────────────┘

内容的提问来源于stack exchange,提问作者Kevin Li

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 04:56:15