Data Transformation with 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 - hourly valuesprice - £ per Megawatt hoursolar - W per square meter

Date - calendar date - 2000-01-01Datetime - date and time - 2000-01-01 12:00:00Time - time of day - 12:00:00Duration - time span - 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() - extract the year.dt.month() - extract the month (1-12).dt.day() - extract the day of month.dt.minute() - extract the minute.dt.offset_by() - adjust datetimes"1h" - forward 1 hour"-2d" - backward 2 days.dt.offset_by() - adjust datetimes"1h" - forward 1 hour"-2d" - backward 2 daysprices.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 |
Data Transformation with Polars