移動與累計聚合

使用 Polars 進行資料轉換

Liam Brannigan

Data Scientist & Polars Contributor

為什麼要用移動統計?

電價折線圖

使用 Polars 進行資料轉換

為什麼要用移動統計?

含移動統計的電價折線圖

使用 Polars 進行資料轉換

對欄位計算移動平均

prices.with_columns(

)
使用 Polars 進行資料轉換

對欄位計算移動平均

prices.with_columns(
    pl.col("price").rolling_mean(            )
)
使用 Polars 進行資料轉換

對欄位計算移動平均

prices.with_columns(
    pl.col("price").rolling_mean(window_size=3)
)
使用 Polars 進行資料轉換

對欄位計算移動平均

prices.with_columns(
    pl.col("price").rolling_mean(window_size=3).alias("smoothed")
)
使用 Polars 進行資料轉換

對欄位計算移動平均

prices.with_columns(
    pl.col("price").rolling_mean(window_size=3).alias("smoothed")
)
| time                | price | solar | smoothed |
| ---                 | ---   | ---   | ---      |
| datetime[µs]        | f64   | f64   | f64      |
|---------------------|-------|-------|----------|
| 2025-07-05 00:00:00 | 34.2  | 0.0   | null     |
| 2025-07-05 01:00:00 | 28.7  | 0.0   | null     |

使用 Polars 進行資料轉換

對欄位計算移動平均

prices.with_columns(
    pl.col("price").rolling_mean(window_size=3).alias("smoothed")
)
| time                | price | solar | smoothed |
| ---                 | ---   | ---   | ---      |
| datetime[µs]        | f64   | f64   | f64      |
|---------------------|-------|-------|----------|
| 2025-07-05 00:00:00 | 34.2  | 0.0   | null     |
| 2025-07-05 01:00:00 | 28.7  | 0.0   | null     |
| 2025-07-05 02:00:00 | 27.5  | 0.0   | 30.1     |
使用 Polars 進行資料轉換

視窗置中

prices.with_columns(
    pl.col("price").rolling_mean(window_size=3,            )
)
使用 Polars 進行資料轉換

視窗置中

prices.with_columns(
    pl.col("price").rolling_mean(window_size=3, center=True).alias("centered")
)
| time                | price | solar | centered |
| ---                 | ---   | ---   | ---      |
| datetime[µs]        | f64   | f64   | f64      |
|---------------------|-------|-------|----------|
| 2025-07-05 00:00:00 | 34.2  | 0.0   | null     |
| 2025-07-05 01:00:00 | 28.7  | 0.0   | 30.1     |
| 2025-07-05 02:00:00 | 27.5  | 0.0   | 27.1     |
使用 Polars 進行資料轉換

其他移動運算式

  • .rolling_sum()-移動總和
  • .rolling_std()-移動標準差
  • .rolling_max().rolling_min()
使用 Polars 進行資料轉換

對 DataFrame 做移動計算

prices.rolling(                                )


使用 Polars 進行資料轉換

對 DataFrame 做移動計算

prices.rolling(index_column="time",            )


使用 Polars 進行資料轉換

對 DataFrame 做移動計算

prices.rolling(index_column="time", period="3h")


使用 Polars 進行資料轉換

對 DataFrame 做移動計算

prices.rolling(index_column="time", period="3h").agg(
    pl.all().mean()
)
使用 Polars 進行資料轉換

對 DataFrame 做移動計算

prices.rolling(index_column="time", period="3h").agg(
    pl.all().mean()
)
| time                | price | solar  |
| ---                 | ---   | ---    |
| datetime[µs]        | f64   | f64    |
|---------------------|-------|------- |
| 2025-07-05 10:00:00 | 1.8   | 247.3  |
| 2025-07-05 11:00:00 | 1.4   | 347.7  |
| 2025-07-05 12:00:00 | 0.9   | 420.7  |
使用 Polars 進行資料轉換

為什麼要用累計統計?

太陽能時間序列圖

  • 任務:計算累計值
使用 Polars 進行資料轉換

為什麼要用累計統計?

太陽能時間序列與累計最大值

使用 Polars 進行資料轉換

累計統計

prices.with_columns(
    pl.col("solar")
)
使用 Polars 進行資料轉換

累計統計

prices.with_columns(
    pl.col("solar").cum_max()
)
使用 Polars 進行資料轉換

累計統計

prices.with_columns(
    pl.col("solar").cum_max().alias("cumulative_solar")
)
使用 Polars 進行資料轉換

累計統計

prices.with_columns(
    pl.col("solar").cum_max().alias("cumulative_solar")
)
| time                | price | solar | cumulative_solar |
| ---                 | ---   | ---   | ---              |
| datetime[µs]        | f64   | f64   | f64              |
|---------------------|-------|-------|------------------|
| ...                 | ...   | ...   | ...              |
| 2025-07-05 10:00:00 | 2.3   | 419.0 | 419.0            |
| 2025-07-05 11:00:00 | 0.5   | 481.0 | 481.0            |
| 2025-07-05 12:00:00 | 0.0   | 462.0 | 481.0            |
| ...                 | ...   | ...   | ...              |
使用 Polars 進行資料轉換

用累計和計算即時總量

prices.with_columns(
    pl.col("solar").cum_sum().alias("total_energy")
)
| time                | price | solar | total_energy |
| ---                 | ---   | ---   | ---          |
| datetime[µs]        | f64   | f64   | f64          |
|---------------------|-------|-------|--------------|
| 2025-07-05 06:00:00 | 22.1  | 62.0  | 62.0         |
| 2025-07-05 07:00:00 | 4.5   | 100.0 | 162.0        |
| 2025-07-05 08:00:00 | 1.7   | 180.0 | 342.0        |
使用 Polars 進行資料轉換

一起來練習吧!

使用 Polars 進行資料轉換

Preparing Video For Download...