Tolka och manipulera datum

Datatransformation med Polars

Liam Brannigan

Data Scientist & Polars Contributor

Tidsseriedata

Tidsserie för marknadspriser

Datatransformation med Polars

Tidsseriedata

Tidsserie för försäljning

Datatransformation med Polars

Tidsseriedata

Tidsserie för streams

Datatransformation med Polars

Tidsseriedata

Tidsserie för antal

Datatransformation med Polars

Datamängd: elpriser

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 - timvärden
  • price - £ per megawattimme
  • solar - W per kvadratmeter
  • solar > 400 = varm sommardag
Datatransformation med Polars

Samband mellan pris och sol

Tidsserie för elpriser

Datatransformation med Polars

Samband mellan pris och sol

Tidsserie för elpriser

Datatransformation med Polars

Datetime-typer i Polars

  • Date - kalenderdatum - 2000-01-01
  • Datetime - datum och tid - 2000-01-01 12:00:00
  • Time - tid på dagen - 12:00:00
  • Duration - tidsperiod - 3 hours
Datatransformation med Polars

Automatisk datetime-tolkning

pl.read_csv(
  "electricity_prices",

)
Datatransformation med Polars

Automatisk datetime-tolkning

pl.read_csv(
  "electricity_prices",
  try_parse_dates=True
)
  • format: YYYY-MM-DD
  • t.ex. 2025-07-05
  • t.ex. 2025-07-05 12:00:00
Datatransformation med Polars

Våra data behöver manuell tolkning

| 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
  • känns inte igen automatiskt av Polars
Datatransformation med Polars

Tolkning med strptime

prices.with_columns(
    pl.col("time").str.strptime(                             )
)
Datatransformation med Polars

Tolkning med strptime

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime,                 )
)
Datatransformation med Polars

Tolkning med strptime

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime, "%d/%m/%Y      ")
)
Datatransformation med Polars

Tolkning med strptime

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

Tolkning med 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   |
Datatransformation med Polars

Extrahera datetime-komponenter

Tabell med tidsseriedata

Datatransformation med Polars

Extrahera datetime-komponenter

Tabell med tidsseriedata där datumet är markerat på en rad

Datatransformation med Polars

Extrahera datetime-komponenter

Tabell med tidsseriedata där tiden är markerad på en rad

Datatransformation med Polars

Extrahera datetime-komponenter

Tabell med tidsseriedata där timmen är markerad på en rad

Datatransformation med Polars

Extrahera datumet

prices.with_columns(
    pl.col("time").dt.date()
)
Datatransformation med Polars

Extrahera datumet

prices.with_columns(
    pl.col("time").dt.date().alias("date")
)
Datatransformation med Polars

Extrahera datumet

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 |
Datatransformation med Polars

Extrahera tiden

prices.with_columns(
    pl.col("time").dt.time().alias("time_of_day")
)
Datatransformation med Polars

Extrahera tiden

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    |
Datatransformation med Polars

Extrahera timmen

prices.with_columns(
    pl.col("time").dt.hour().alias("hour")
)
Datatransformation med Polars

Extrahera timmen

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   |
Datatransformation med Polars

Övriga datetime-komponenter

  • .dt.year() - extrahera året
  • .dt.month() - extrahera månaden (1–12)
  • .dt.day() - extrahera dagen i månaden
  • .dt.minute() - extrahera minuten
  • ...
1 https://docs.pola.rs/api/python/dev/reference/expressions/temporal.html
Datatransformation med Polars

Justera datum och tid

  • .dt.offset_by() - justera datum och tid
  • "1h" - framåt 1 timme
  • "-2d" - bakåt 2 dagar
Datatransformation med Polars

Justera datum och tid

  • .dt.offset_by() - justera datum och tid
  • "1h" - framåt 1 timme
  • "-2d" - bakåt 2 dagar
1 https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.dt.offset_by.html
Datatransformation med Polars

Lägg till ett tidsfönstrets slut

prices.with_columns(
    pl.col("time").dt.offset_by("1h")
)
Datatransformation med Polars

Lägg till ett tidsfönstrets slut

prices.with_columns(
    pl.col("time").dt.offset_by("1h").alias("time_end")
)
Datatransformation med Polars

Lägg till ett tidsfönstrets slut

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 |
Datatransformation med Polars

Nu kör vi en övning!

Datatransformation med Polars

Preparing Video For Download...