Parsarea și manipularea datelor calendaristice

Transformarea datelor cu Polars

Liam Brannigan

Data Scientist & Polars Contributor

Date de tip serie de timp

Serii de timp pentru prețuri de piață

Transformarea datelor cu Polars

Date de tip serie de timp

Serii de timp pentru vânzări

Transformarea datelor cu Polars

Date de tip serie de timp

Serii de timp pentru redări

Transformarea datelor cu Polars

Date de tip serie de timp

Serii de timp pentru număr de înregistrări

Transformarea datelor cu Polars

Setul de date despre prețurile la electricitate

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 - valori orare
  • price - £ per megawatt-oră
  • solar - W pe metru pătrat
  • solar > 400 = zi caldă de vară
Transformarea datelor cu Polars

Relația dintre preț și energia solară

Serii de timp ale prețurilor la electricitate

Transformarea datelor cu Polars

Relația dintre preț și energia solară

Serii de timp ale prețurilor la electricitate

Transformarea datelor cu Polars

Tipuri datetime în Polars

  • Date - dată calendaristică - 2000-01-01
  • Datetime - dată și oră - 2000-01-01 12:00:00
  • Time - oră din zi - 12:00:00
  • Duration - interval de timp - 3 hours
Transformarea datelor cu Polars

Parsare automată a datelor calendaristice

pl.read_csv(
  "electricity_prices",

)
Transformarea datelor cu Polars

Parsare automată a datelor calendaristice

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

Datele noastre necesită parsare manuală

| 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
  • nedetectat automat de Polars
Transformarea datelor cu Polars

Parsare cu strptime

prices.with_columns(
    pl.col("time").str.strptime(                             )
)
Transformarea datelor cu Polars

Parsare cu strptime

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime,                 )
)
Transformarea datelor cu Polars

Parsare cu strptime

prices.with_columns(
    pl.col("time").str.strptime(pl.Datetime, "%d/%m/%Y      ")
)
Transformarea datelor cu Polars

Parsare cu strptime

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

Parsare cu 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   |
Transformarea datelor cu Polars

Extragerea componentelor datetime

Tabel cu date de tip serie de timp

Transformarea datelor cu Polars

Extragerea componentelor datetime

Tabel cu date de tip serie de timp, cu data evidențiată pe un rând

Transformarea datelor cu Polars

Extragerea componentelor datetime

Tabel cu date de tip serie de timp, cu ora evidențiată pe un rând

Transformarea datelor cu Polars

Extragerea componentelor datetime

Tabel cu date de tip serie de timp, cu ora exactă evidențiată pe un rând

Transformarea datelor cu Polars

Extragerea datei calendaristice

prices.with_columns(
    pl.col("time").dt.date()
)
Transformarea datelor cu Polars

Extragerea datei calendaristice

prices.with_columns(
    pl.col("time").dt.date().alias("date")
)
Transformarea datelor cu Polars

Extragerea datei calendaristice

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 |
Transformarea datelor cu Polars

Extragerea orei

prices.with_columns(
    pl.col("time").dt.time().alias("time_of_day")
)
Transformarea datelor cu Polars

Extragerea orei

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    |
Transformarea datelor cu Polars

Extragerea orei exacte

prices.with_columns(
    pl.col("time").dt.hour().alias("hour")
)
Transformarea datelor cu Polars

Extragerea orei exacte

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   |
Transformarea datelor cu Polars

Alte componente datetime

  • .dt.year() - extrage anul
  • .dt.month() - extrage luna (1-12)
  • .dt.day() - extrage ziua din lună
  • .dt.minute() - extrage minutul
  • ...
1 https://docs.pola.rs/api/python/dev/reference/expressions/temporal.html
Transformarea datelor cu Polars

Ajustarea valorilor datetime

  • .dt.offset_by() - ajustează valorile datetime
  • "1h" - înainte cu 1 oră
  • "-2d" - înapoi cu 2 zile
Transformarea datelor cu Polars

Ajustarea valorilor datetime

  • .dt.offset_by() - ajustează valorile datetime
  • "1h" - înainte cu 1 oră
  • "-2d" - înapoi cu 2 zile
1 https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.dt.offset_by.html
Transformarea datelor cu Polars

Adăugarea sfârșitului unui interval de timp

prices.with_columns(
    pl.col("time").dt.offset_by("1h")
)
Transformarea datelor cu Polars

Adăugarea sfârșitului unui interval de timp

prices.with_columns(
    pl.col("time").dt.offset_by("1h").alias("time_end")
)
Transformarea datelor cu Polars

Adăugarea sfârșitului unui interval de timp

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 |
Transformarea datelor cu Polars

Hai să exersăm!

Transformarea datelor cu Polars

Preparing Video For Download...