Agregasi rolling dan kumulatif

Transformasi Data dengan Polars

Liam Brannigan

Data Scientist & Polars Contributor

Mengapa statistik rolling?

Plot garis harga listrik

Transformasi Data dengan Polars

Mengapa statistik rolling?

Plot garis harga listrik dengan statistik rolling

Transformasi Data dengan Polars

Rata-rata rolling pada kolom

prices.with_columns(

)
Transformasi Data dengan Polars

Rata-rata rolling pada kolom

prices.with_columns(
    pl.col("price").rolling_mean(            )
)
Transformasi Data dengan Polars

Rata-rata rolling pada kolom

prices.with_columns(
    pl.col("price").rolling_mean(window_size=3)
)
Transformasi Data dengan Polars

Rata-rata rolling pada kolom

prices.with_columns(
    pl.col("price").rolling_mean(window_size=3).alias("smoothed")
)
Transformasi Data dengan Polars

Rata-rata rolling pada kolom

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     |

Transformasi Data dengan Polars

Rata-rata rolling pada kolom

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     |
Transformasi Data dengan Polars

Memusatkan jendela

prices.with_columns(
    pl.col("price").rolling_mean(window_size=3,            )
)
Transformasi Data dengan Polars

Memusatkan jendela

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     |
Transformasi Data dengan Polars

Ekspresi rolling lainnya

  • .rolling_sum() - total berjalan
  • .rolling_std() - simpangan baku berjalan
  • .rolling_max() / .rolling_min()
Transformasi Data dengan Polars

Rolling pada DataFrame

prices.rolling(                                )


Transformasi Data dengan Polars

Rolling pada DataFrame

prices.rolling(index_column="time",            )


Transformasi Data dengan Polars

Rolling pada DataFrame

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


Transformasi Data dengan Polars

Rolling pada DataFrame

prices.rolling(index_column="time", period="3h").agg(
    pl.all().mean()
)
Transformasi Data dengan Polars

Rolling pada 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  |
Transformasi Data dengan Polars

Mengapa statistik kumulatif?

Grafik deret waktu daya surya

  • Tugas: hitung nilai kumulatif
Transformasi Data dengan Polars

Mengapa statistik kumulatif?

Grafik deret waktu daya surya dengan maksimum kumulatif daya surya

Transformasi Data dengan Polars

Statistik kumulatif

prices.with_columns(
    pl.col("solar")
)
Transformasi Data dengan Polars

Statistik kumulatif

prices.with_columns(
    pl.col("solar").cum_max()
)
Transformasi Data dengan Polars

Statistik kumulatif

prices.with_columns(
    pl.col("solar").cum_max().alias("cumulative_solar")
)
Transformasi Data dengan Polars

Statistik kumulatif

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            |
| ...                 | ...   | ...   | ...              |
Transformasi Data dengan Polars

Jumlah kumulatif untuk total berjalan

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        |
Transformasi Data dengan Polars

Ayo berlatih!

Transformasi Data dengan Polars

Preparing Video For Download...