Parsowanie i manipulowanie datami

Transformacje danych z Polars

Liam Brannigan

Data Scientist & Polars Contributor

Dane szeregów czasowych

Szereg czasowy cen rynkowych

Transformacje danych z Polars

Dane szeregów czasowych

Szereg czasowy sprzedaży

Transformacje danych z Polars

Dane szeregów czasowych

Szereg czasowy liczby odtworzeń

Transformacje danych z Polars

Dane szeregów czasowych

Szereg czasowy liczby zdarzeń

Transformacje danych z Polars

Zbiór danych — ceny energii elektrycznej

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 - wartości godzinowe
  • price - £ za megawatogodzinę
  • solar - W na metr kwadratowy
  • solar > 400 = ciepły letni dzień
Transformacje danych z Polars

Zależność między ceną a nasłonecznieniem

Szereg czasowy cen energii elektrycznej

Transformacje danych z Polars

Zależność między ceną a nasłonecznieniem

Szereg czasowy cen energii elektrycznej

Transformacje danych z Polars

Typy danych datetime w Polars

  • Date - data kalendarzowa - 2000-01-01
  • Datetime - data i czas - 2000-01-01 12:00:00
  • Time - pora dnia - 12:00:00
  • Duration - przedział czasu - 3 hours
Transformacje danych z Polars

Automatyczne parsowanie dat

pl.read_csv(
  "electricity_prices",

)
Transformacje danych z Polars

Automatyczne parsowanie dat

pl.read_csv(
  "electricity_prices",
  try_parse_dates=True
)
  • format: YYYY-MM-DD
  • np. 2025-07-05
  • np. 2025-07-05 12:00:00
Transformacje danych z Polars

Nasze dane wymagają ręcznego parsowania

| 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
  • Polars nie wykrywa go automatycznie
Transformacje danych z Polars

Parsowanie za pomocą strptime

prices.with_columns(
    pl.col("time").str.strptime(                             )
)
Transformacje danych z Polars

Parsowanie za pomocą strptime

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime,                 )
)
Transformacje danych z Polars

Parsowanie za pomocą strptime

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime, "%d/%m/%Y      ")
)
Transformacje danych z Polars

Parsowanie za pomocą strptime

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

Parsowanie za pomocą 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   |
Transformacje danych z Polars

Wyodrębnianie składników datetime

Tabela z danymi szeregów czasowych

Transformacje danych z Polars

Wyodrębnianie składników datetime

Tabela z danymi szeregów czasowych z podświetloną datą w jednym wierszu

Transformacje danych z Polars

Wyodrębnianie składników datetime

Tabela z danymi szeregów czasowych z podświetlonym czasem w jednym wierszu

Transformacje danych z Polars

Wyodrębnianie składników datetime

Tabela z danymi szeregów czasowych z podświetloną godziną w jednym wierszu

Transformacje danych z Polars

Wyodrębnianie daty

prices.with_columns(
    pl.col("time").dt.date()
)
Transformacje danych z Polars

Wyodrębnianie daty

prices.with_columns(
    pl.col("time").dt.date().alias("date")
)
Transformacje danych z Polars

Wyodrębnianie daty

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 |
Transformacje danych z Polars

Wyodrębnianie czasu

prices.with_columns(
    pl.col("time").dt.time().alias("time_of_day")
)
Transformacje danych z Polars

Wyodrębnianie czasu

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    |
Transformacje danych z Polars

Wyodrębnianie godziny

prices.with_columns(
    pl.col("time").dt.hour().alias("hour")
)
Transformacje danych z Polars

Wyodrębnianie godziny

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   |
Transformacje danych z Polars

Pozostałe składniki datetime

  • .dt.year() - wyodrębnia rok
  • .dt.month() - wyodrębnia miesiąc (1–12)
  • .dt.day() - wyodrębnia dzień miesiąca
  • .dt.minute() - wyodrębnia minuty
  • ...
1 https://docs.pola.rs/api/python/dev/reference/expressions/temporal.html
Transformacje danych z Polars

Przesuwanie wartości datetime

  • .dt.offset_by() - przesuwa wartości datetime
  • "1h" - o 1 godzinę do przodu
  • "-2d" - o 2 dni do tyłu
Transformacje danych z Polars

Przesuwanie wartości datetime

  • .dt.offset_by() - przesuwa wartości datetime
  • "1h" - o 1 godzinę do przodu
  • "-2d" - o 2 dni do tyłu
1 https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.dt.offset_by.html
Transformacje danych z Polars

Dodawanie końca okna czasowego

prices.with_columns(
    pl.col("time").dt.offset_by("1h")
)
Transformacje danych z Polars

Dodawanie końca okna czasowego

prices.with_columns(
    pl.col("time").dt.offset_by("1h").alias("time_end")
)
Transformacje danych z Polars

Dodawanie końca okna czasowego

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 |
Transformacje danych z Polars

Czas na praktykę!

Transformacje danych z Polars

Preparing Video For Download...