日付の解析と操作

Data Transformation with Polars

Liam Brannigan

Data Scientist & Polars Contributor

時系列データ

市場価格の時系列

Data Transformation with Polars

時系列データ

売上の時系列

Data Transformation with Polars

時系列データ

ストリーム数の時系列

Data Transformation with Polars

時系列データ

カウントの時系列

Data Transformation with Polars

電力価格データセット

shape: (24, 3)
| time             | price | solar |
| ---              | ---   | ---   |
| str              | f64   | f64   |
|------------------|-------|-------|
| 05/07/2025 00:00 | 34.2  | 0.0   |
| ...              | ...   | ...   |
| 05/07/2025 12:00 | 0.0   | 462.0 |
  • time - 毎時の値
  • price - £/MWh
  • solar - W/平方メートル
  • solar > 400 = 暖かい夏の日
Data Transformation with Polars

価格と太陽光の関係

電力価格の時系列

Data Transformation with Polars

価格と太陽光の関係

電力価格の時系列

Data Transformation with Polars

Polars の日時型

  • Date - 日付 - 2000-01-01
  • Datetime - 日時 - 2000-01-01 12:00:00
  • Time - 時刻 - 12:00:00
  • Duration - 期間 - 3 hours
Data Transformation with Polars

日時の自動解析

pl.read_csv(
  "electricity_prices",

)
Data Transformation with Polars

日時の自動解析

pl.read_csv(
  "electricity_prices",
  try_parse_dates=True
)
  • 形式: YYYY-MM-DD
  • 例: 2025-07-05
  • 例: 2025-07-05 12:00:00
Data Transformation with Polars

このデータは手動解析が必要

| time             | price | solar |
| ---              | ---   | ---   |
| str              | f64   | f64   |
|------------------|-------|-------|
| 05/07/2025 00:00 | 34.2  | 0.0   |
| 05/07/2025 01:00 | 28.7  | 0.0   |
  • 形式: DD/MM/YYYY HH:MM
  • Polars は自動検出しない
Data Transformation with Polars

strptime で解析

prices.with_columns(
    pl.col("time").str.strptime(                             )
)
Data Transformation with Polars

strptime で解析

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime,                 )
)
Data Transformation with Polars

strptime で解析

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime, "%d/%m/%Y      ")
)
Data Transformation with Polars

strptime で解析

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime, "%d/%m/%Y %H:%M")
)
Data Transformation with Polars

strptime で解析

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime, "%d/%m/%Y %H:%M")
)
| time                | price | solar |
| ---                 | ---   | ---   |
| datetime[us]        | 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 02:00:00 | 27.5  | 0.0   |
Data Transformation with Polars

日時成分の抽出

時系列データの表

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.date()
)
Data Transformation with Polars

日付の抽出

prices.with_columns(
    pl.col("time").dt.date().alias("date")
)
Data Transformation with Polars

日付の抽出

prices.with_columns(
    pl.col("time").dt.date().alias("date")
)
| time                | price | solar | date       |
| ---                 | ---   | ---   | ---        |
| datetime[µs]        | f64   | f64   | date       |
|---------------------|-------|-------|------------|
| 2025-07-05 00:00:00 | 34.2  | 0.0   | 2025-07-05 |
| 2025-07-05 01:00:00 | 28.7  | 0.0   | 2025-07-05 |
| 2025-07-05 02:00:00 | 27.5  | 0.0   | 2025-07-05 |
Data Transformation with Polars

時刻の抽出

prices.with_columns(
    pl.col("time").dt.time().alias("time_of_day")
)
Data Transformation with Polars

時刻の抽出

prices.with_columns(
    pl.col("time").dt.time().alias("time_of_day")
)
| time                | price | solar | time_of_day |
| ---                 | ---   | ---   | ---         |
| datetime[us]        | f64   | f64   | time        |
|---------------------|-------|-------|-------------|
| 2025-07-05 00:00:00 | 34.2  | 0.0   | 00:00:00    |
| 2025-07-05 01:00:00 | 28.7  | 0.0   | 01:00:00    |
| 2025-07-05 02:00:00 | 27.5  | 0.0   | 02:00:00    |
Data Transformation with Polars

時間の抽出

prices.with_columns(
    pl.col("time").dt.hour().alias("hour")
)
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 00:00:00 | 34.2  | 0.0   | 0    |
| 2025-07-05 01:00:00 | 28.7  | 0.0   | 1    |
| 2025-07-05 12:00:00 | 0.0   | 462.0 | 12   |
Data Transformation with Polars

その他の日時成分

  • .dt.year() - 年を抽出
  • .dt.month() - 月を抽出 (1-12)
  • .dt.day() - 日を抽出
  • .dt.minute() - 分を抽出
  • ...
1 https://docs.pola.rs/api/python/dev/reference/expressions/temporal.html
Data Transformation with Polars

日時の調整

  • .dt.offset_by() - 日時を調整
  • "1h" - 1 時間進める
  • "-2d" - 2 日戻す
Data Transformation with Polars

日時の調整

  • .dt.offset_by() - 日時を調整
  • "1h" - 1 時間進める
  • "-2d" - 2 日戻す
1 https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.dt.offset_by.html
Data Transformation with Polars

時間窓の終了を追加

prices.with_columns(
    pl.col("time").dt.offset_by("1h")
)
Data Transformation with Polars

時間窓の終了を追加

prices.with_columns(
    pl.col("time").dt.offset_by("1h").alias("time_end")
)
Data Transformation with Polars

時間窓の終了を追加

prices.with_columns(
    pl.col("time").dt.offset_by("1h").alias("time_end")
)
| time                | price | solar | time_end            |
| ---                 | ---   | ---   | ---                 |
| datetime[µs]        | f64   | f64   | datetime[µs]        |
|---------------------|-------|-------|---------------------|
| 2025-07-05 00:00:00 | 34.2  | 0.0   | 2025-07-05 01:00:00 |
| 2025-07-05 01:00:00 | 28.7  | 0.0   | 2025-07-05 02:00:00 |
| 2025-07-05 02:00:00 | 27.5  | 0.0   | 2025-07-05 03:00:00 |
Data Transformation with Polars

Passons à la pratique !

Data Transformation with Polars

Preparing Video For Download...