グループごとの変換

Data Transformation with Polars

Liam Brannigan

Data Scientist & Polars Contributor

なぜグループ統計と比較するのか

1日の電気料金の時系列チャート

  • 価格は通常より高いか低いか?
Data Transformation with Polars

なぜグループ統計と比較するのか

1日の電気料金と時間帯平均の時系列チャート

Data Transformation with Polars

なぜグループ統計と比較するのか

1日の電気料金と時間帯平均の時系列チャート。正午の安値を注記

Data Transformation with Polars

なぜグループ統計と比較するのか

1日の電気料金と時間帯平均の時系列チャート。夕方は通常価格と注記

Data Transformation with Polars

時を抽出する

prices.with_columns(
    pl.col("time").dt.hour().alias("hour")
)
| time                | price | solar   | hour |
| ---                 | ---   | ---     | ---  |
| datetime[µs]        | f64   | f64     | i8   |
|---------------------|-------|-------  |------|
| 2025-07-05 10:00:00 | 2.3   | 419.0   | 10   |
| 2025-07-05 11:00:00 | 0.5   | 481.0   | 11   |
| 2025-07-05 12:00:00 | 0.0   | 462.0   | 12   |
Data Transformation with Polars

.over() を使うウィンドウ関数

prices.with_columns(
    pl.col("price").mean()
)
Data Transformation with Polars

.over() を使うウィンドウ関数

prices.with_columns(
    pl.col("price").mean().over("hour")
)
Data Transformation with Polars

.over() を使うウィンドウ関数

prices.with_columns(
    pl.col("price").mean().over("hour").alias("hourly_avg")
)
Data Transformation with Polars

.over() を使うウィンドウ関数

prices.with_columns(
    pl.col("price").mean().over("hour").alias("hourly_avg")
)
| time                | price | solar | hour | hourly_avg |
| ---                 | ---   | ---   | ---  | ---        |
| datetime[µs]        | f64   | f64   | i8   | f64        |
|---------------------|-------|-------|------|------------|
| 2025-07-05 10:00:00 | 2.3   | 419.0 | 10   | 49.6       |
| 2025-07-05 11:00:00 | 0.5   | 481.0 | 11   | 44.4       |
| 2025-07-05 12:00:00 | 0.0   | 462.0 | 12   | 39.5       |
Data Transformation with Polars

天気カテゴリの作成

  • 晴れ: solar > 400 W/m²
  • くもり: solar <= 400 W/m²
Data Transformation with Polars

天気カテゴリの作成

prices.with_columns(
    pl.when(pl.col("solar") > 400)
    .then(pl.lit("sunny"))
    .otherwise(pl.lit("cloudy"))

.alias("weather")
)
| time                | price | solar | weather |
| ---                 | ---   | ---   | ---     |
| datetime[µs]        | f64   | f64   | str     |
|---------------------|-------|-------|---------|
| 2025-07-05 10:00:00 | 2.3   | 419.0 | sunny   |
| 2025-07-06 10:00:00 | 80.5  | 181.0 | cloudy  |
| 2025-07-07 10:00:00 | 41.5  | 150.0 | cloudy  |
Data Transformation with Polars

複数列でのグループ化

prices.with_columns(
    pl.col("price").mean()
)
Data Transformation with Polars

複数列でのグループ化

prices.with_columns(
    pl.col("price").mean().over("hour", "weather")
)
Data Transformation with Polars

複数列でのグループ化

prices.with_columns(
    pl.col("price").mean().over("hour", "weather").alias("avg_by_hour_weather")
)
Data Transformation with Polars

複数列でのグループ化

prices.with_columns(
    pl.col("price").mean().over("hour", "weather").alias("avg_by_hour_weather")
)
| time                | price | hour | weather | avg_by_hour_weather |
| ---                 | ---   | ---  | ---     | ---                 |
| datetime[µs]        | f64   | i8   | str     | f64                 |
|---------------------|-------|------|---------|---------------------|
| 2025-07-05 10:00:00 | 2.3   | 10   | sunny   | 34.2                |
| 2025-07-06 10:00:00 | 80.5  | 10   | cloudy  | 58.0                |
| 2025-07-07 10:00:00 | 41.5  | 10   | cloudy  | 58.0                |
Data Transformation with Polars

グループ平均との比較

prices.with_columns(
    (pl.col("price")                                     )

)
Data Transformation with Polars

グループ平均との比較

prices.with_columns(
    (pl.col("price") - pl.col("price").mean().over("hour"))

)
Data Transformation with Polars

グループ平均との比較

prices.with_columns(
    (pl.col("price") - pl.col("price").mean().over("hour"))
    .alias("diff_from_hourly_avg")
)
| time                | price | hour | diff_from_hourly_avg |
| ---                 | ---   | ---  | ---                  |
| datetime[µs]        | f64   | i8   | f64                  |
|---------------------|-------|------|----------------------|
| 2025-07-05 10:00:00 | 2.3   | 10   | -47.3                |
| 2025-07-06 10:00:00 | 25.1  | 10   | -24.5                |
| 2025-07-07 10:00:00 | 2.7   | 10   | -46.9                |
Data Transformation with Polars

他のウィンドウ集計

.over() は多くの集計で使えます。

  • .sum().over(): グループ合計
  • .max().over(): グループ最大
  • .count().over(): グループ件数
Data Transformation with Polars

Passons à la pratique !

Data Transformation with Polars

Preparing Video For Download...