Datentransformation mit Polars
Liam Brannigan
Data Scientist & Polars Contributor




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 Werteprice – £ pro Megawattstundesolar – W pro Quadratmeter

Date – Kalenderdatum – 2000-01-01Datetime – Datum und Uhrzeit – 2000-01-01 12:00:00Time – Tageszeit – 12:00:00Duration – Zeitspanne – 3 hourspl.read_csv(
"electricity_prices",
)
pl.read_csv(
"electricity_prices",
try_parse_dates=True
)
YYYY-MM-DD2025-07-052025-07-05 12:00:00| 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:MMprices.with_columns(
pl.col("time").str.strptime( )
)
prices.with_columns(
pl.col("time").str.strptime(pl.Datetime, )
)
prices.with_columns(
pl.col("time").str.strptime(pl.Datetime, "%d/%m/%Y ")
)
prices.with_columns(
pl.col("time").str.strptime(pl.Datetime, "%d/%m/%Y %H:%M")
)
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 |




prices.with_columns(
pl.col("time").dt.date()
)
prices.with_columns(
pl.col("time").dt.date().alias("date")
)
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 |
prices.with_columns(
pl.col("time").dt.time().alias("time_of_day")
)
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 |
prices.with_columns(
pl.col("time").dt.hour().alias("hour")
)
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 |
.dt.year() – Jahr extrahieren.dt.month() – Monat extrahieren (1–12).dt.day() – Tag des Monats extrahieren.dt.minute() – Minute extrahieren.dt.offset_by() – Datetimes anpassen"1h" – 1 Stunde vorwärts"-2d" – 2 Tage rückwärts.dt.offset_by() – Datetimes anpassen"1h" – 1 Stunde vorwärts"-2d" – 2 Tage rückwärtsprices.with_columns(
pl.col("time").dt.offset_by("1h")
)
prices.with_columns(
pl.col("time").dt.offset_by("1h").alias("time_end")
)
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 |
Datentransformation mit Polars