Datumsangaben parsen und bearbeiten

Daten­transformation mit Polars

Liam Brannigan

Data Scientist & Polars Contributor

Zeitreihendaten

Zeitreihe der Marktpreise

Daten­transformation mit Polars

Zeitreihendaten

Zeitreihe der Verkäufe

Daten­transformation mit Polars

Zeitreihendaten

Zeitreihe der Streams

Daten­transformation mit Polars

Zeitreihendaten

Zeitreihe der Anzahl

Daten­transformation mit Polars

Datensatz zu Strompreisen

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 – stündliche Werte
  • price – £ pro Megawattstunde
  • solar – W pro Quadratmeter
  • solar > 400 = warmer Sommertag
Daten­transformation mit Polars

Zusammenhang zwischen Preis und Solar

Zeitreihe der Strompreise

Daten­transformation mit Polars

Zusammenhang zwischen Preis und Solar

Zeitreihe der Strompreise

Daten­transformation mit Polars

Datetime-Datentypen in Polars

  • Date – Kalenderdatum – 2000-01-01
  • Datetime – Datum und Uhrzeit – 2000-01-01 12:00:00
  • Time – Tageszeit – 12:00:00
  • Duration – Zeitspanne – 3 hours
Daten­transformation mit Polars

Automatisches Datetime-Parsing

pl.read_csv(
  "electricity_prices",

)
Daten­transformation mit Polars

Automatisches Datetime-Parsing

pl.read_csv(
  "electricity_prices",
  try_parse_dates=True
)
  • Format: YYYY-MM-DD
  • z. B. 2025-07-05
  • z. B. 2025-07-05 12:00:00
Daten­transformation mit Polars

Unsere Daten brauchen manuelles Parsing

| time             | price | solar |
| ---              | ---   | ---   |
| str              | f64   | f64   |
|------------------|-------|-------|
| 05/07/2025 00:00 | 34.2  | 0.0   |
| 05/07/2025 01:00 | 28.7  | 0.0   |
  • Format: DD/MM/YYYY HH:MM
  • wird von Polars nicht automatisch erkannt
Daten­transformation mit Polars

Parsen mit strptime

prices.with_columns(
    pl.col("time").str.strptime(                             )
)
Daten­transformation mit Polars

Parsen mit strptime

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime,                 )
)
Daten­transformation mit Polars

Parsen mit strptime

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime, "%d/%m/%Y      ")
)
Daten­transformation mit Polars

Parsen mit strptime

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime, "%d/%m/%Y %H:%M")
)
Daten­transformation mit Polars

Parsen mit 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   |
Daten­transformation mit Polars

Datetime-Komponenten extrahieren

Tabelle mit Zeitreihendaten

Daten­transformation mit Polars

Datetime-Komponenten extrahieren

Tabelle mit Zeitreihendaten, Datum in einer Zeile hervorgehoben

Daten­transformation mit Polars

Datetime-Komponenten extrahieren

Tabelle mit Zeitreihendaten, Uhrzeit in einer Zeile hervorgehoben

Daten­transformation mit Polars

Datetime-Komponenten extrahieren

Tabelle mit Zeitreihendaten, Stunde in einer Zeile hervorgehoben

Daten­transformation mit Polars

Datum extrahieren

prices.with_columns(
    pl.col("time").dt.date()
)
Daten­transformation mit Polars

Datum extrahieren

prices.with_columns(
    pl.col("time").dt.date().alias("date")
)
Daten­transformation mit Polars

Datum extrahieren

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 |
Daten­transformation mit Polars

Uhrzeit extrahieren

prices.with_columns(
    pl.col("time").dt.time().alias("time_of_day")
)
Daten­transformation mit Polars

Uhrzeit extrahieren

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    |
Daten­transformation mit Polars

Stunde extrahieren

prices.with_columns(
    pl.col("time").dt.hour().alias("hour")
)
Daten­transformation mit Polars

Stunde extrahieren

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   |
Daten­transformation mit Polars

Weitere Datetime-Komponenten

  • .dt.year() – Jahr extrahieren
  • .dt.month() – Monat extrahieren (1–12)
  • .dt.day() – Tag des Monats extrahieren
  • .dt.minute() – Minute extrahieren
  • ...
1 https://docs.pola.rs/api/python/dev/reference/expressions/temporal.html
Daten­transformation mit Polars

Datetimes anpassen

  • .dt.offset_by() – Datetimes anpassen
  • "1h" – 1 Stunde vorwärts
  • "-2d" – 2 Tage rückwärts
Daten­transformation mit Polars

Datetimes anpassen

  • .dt.offset_by() – Datetimes anpassen
  • "1h" – 1 Stunde vorwärts
  • "-2d" – 2 Tage rückwärts
1 https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.dt.offset_by.html
Daten­transformation mit Polars

Ende des Zeitfensters hinzufügen

prices.with_columns(
    pl.col("time").dt.offset_by("1h")
)
Daten­transformation mit Polars

Ende des Zeitfensters hinzufügen

prices.with_columns(
    pl.col("time").dt.offset_by("1h").alias("time_end")
)
Daten­transformation mit Polars

Ende des Zeitfensters hinzufügen

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 |
Daten­transformation mit Polars

Lass uns üben!

Daten­transformation mit Polars

Preparing Video For Download...