期間でのフィルタリングと集計

Data Transformation with Polars

Liam Brannigan

Data Scientist & Polars Contributor

特定の価格を探す

時系列データの表

Data Transformation with Polars

特定の価格を探す

1行をハイライトした時系列データの表

Data Transformation with Polars

特定の価格を探す

3行をハイライトした時系列データの表

Data Transformation with Polars

pl.datetime でのフィルタリング

prices.filter(

)

$$

$$

$$

$$

  • タスク:7月5日正午の価格を取得
Data Transformation with Polars

pl.datetime でのフィルタリング

prices.filter(
    pl.col("time") == 
)
Data Transformation with Polars

pl.datetime でのフィルタリング

prices.filter(
    pl.col("time") == pl.datetime(                                  )
)
Data Transformation with Polars

pl.datetime でのフィルタリング

prices.filter(
    pl.col("time") == pl.datetime(year=2025, month=7, day=5, hour=12)
)
| time                | price | solar |
| ---                 | ---   | ---   |
| datetime[µs]        | f64   | f64   |
|---------------------|-------|-------|
| 2025-07-05 12:00:00 | 0.0   | 462.0 |
Data Transformation with Polars

pl.date で日付フィルタリング

prices.filter(
    pl.col("time").dt.date() == 
)
Data Transformation with Polars

pl.date で日付フィルタリング

prices.filter(
    pl.col("time").dt.date() == pl.date(year=2025, month=7, day=5)
)
Data Transformation with Polars

pl.date で日付フィルタリング

prices.filter(
    pl.col("time").dt.date() == pl.date(year=2025, month=7, day=5)
)
| time                | price | solar |
| ---                 | ---   | ---   |
| datetime[µs]        | f64   | f64   |
|---------------------|-------|-------|
| 2025-07-05 00:00:00 | 34.2  | 0.0   |
| 2025-07-05 01:00:00 | 28.7  | 0.0   |
| ...                 | ...   | ...   |
| 2025-07-05 23:00:00 | 72.3  | 0.0   |
Data Transformation with Polars

日時範囲でのフィルタリング

prices.filter(
    pl.col("time").is_between(


    )
)
Data Transformation with Polars

日時範囲でのフィルタリング

prices.filter(
    pl.col("time").is_between(
        pl.datetime(2025, 7, 5, 8),
        pl.datetime(2025, 7, 5, 18)
    )
)
Data Transformation with Polars

日時範囲でのフィルタリング

prices.filter(
    pl.col("time").is_between(
        pl.datetime(2025, 7, 5, 8),
        pl.datetime(2025, 7, 5, 18)
    )
)
| time                | price | solar |
| ---                 | ---   | ---   |
| datetime[µs]        | f64   | f64   |
|---------------------|-------|-------|
| 2025-07-05 08:00:00 | 1.7   | 180.0 |
| 2025-07-05 09:00:00 | 1.3   | 243.0 |
| ...                 | ...   | ...   |
| 2025-07-05 17:00:00 | 77.8  | 153.0 |
| 2025-07-05 18:00:00 | 87.3  | 119.0 |
Data Transformation with Polars

期間での集計

時間ウィンドウに集計する行

  • .group_by_dynamic() で集計
Data Transformation with Polars

期間での集計

prices.sort("time")


Data Transformation with Polars

期間での集計

prices.sort("time").group_by_dynamic("time",          )


Data Transformation with Polars

期間での集計

prices.sort("time").group_by_dynamic("time", every="6h")


Data Transformation with Polars

6時間ウィンドウで集計

prices.sort("time").group_by_dynamic("time", every="6h").agg(

)
Data Transformation with Polars

6時間ウィンドウで集計

prices.sort("time").group_by_dynamic("time", every="6h").agg(
    pl.col("price").mean().alias("avg_price")
)
Data Transformation with Polars

6時間ウィンドウで集計

prices.sort("time").group_by_dynamic("time", every="6h").agg(
    pl.col("price").mean().alias("avg_price")
)
| time                | avg_price |
| ---                 | ---       |
| datetime[µs]        | f64       |
|---------------------|-----------|
| 2025-07-05 00:00:00 | 28.4      |
| 2025-07-05 06:00:00 | 15.2      |
| 2025-07-05 12:00:00 | 42.1      |
| 2025-07-05 18:00:00 | 68.7      |
Data Transformation with Polars

日次で集計

prices.sort("time").group_by_dynamic("time", every="1d").agg(

pl.col("price").mean().alias("avg_price"), pl.col("solar").mean().alias("avg_solar")
)
Data Transformation with Polars

日次で集計

prices.sort("time").group_by_dynamic("time", every="1d").agg(
    pl.col("price").mean().alias("avg_price"),
    pl.col("solar").mean().alias("avg_solar")
)
| time                | avg_price | avg_solar |
| ---                 | ---       | ---       |
| datetime[µs]        | f64       | f64       |
|---------------------|-----------|-----------|
| 2025-07-05 00:00:00 | 36.7      | 147.1     |
| 2025-07-06 00:00:00 | 42.1      | 133.8     |
Data Transformation with Polars

ウィンドウサイズの制御

  • every:新しいウィンドウの開始間隔
  • period:各ウィンドウの長さ
  • 既定では periodevery と同じ
Data Transformation with Polars

ウィンドウサイズの制御

prices.group_by_dynamic("time", every="2h")
Data Transformation with Polars

ウィンドウサイズの制御

非重複の2時間ウィンドウ

Data Transformation with Polars

ウィンドウサイズの制御

prices.group_by_dynamic("time", every="1h", period="2h")
Data Transformation with Polars

ウィンドウサイズの制御

10時開始の最初の重複ウィンドウ

Data Transformation with Polars

ウィンドウサイズの制御

11時開始の2つ目の重複ウィンドウ

Data Transformation with Polars

Passons à la pratique !

Data Transformation with Polars

Preparing Video For Download...