Transformation de données avec 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 - valeurs horairesprice - £ par mégawattheuresolar - W par mètre carré

Date - date du calendrier - 2000-01-01Datetime - date et heure - 2000-01-01 12:00:00Time - heure du jour - 12:00:00Duration - durée - 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() - extrait l’année.dt.month() - extrait le mois (1-12).dt.day() - extrait le jour du mois.dt.minute() - extrait les minutes.dt.offset_by() - ajuster des datetime"1h" - avancer de 1 heure"-2d" - reculer de 2 jours.dt.offset_by() - ajuster des datetime"1h" - avancer de 1 heure"-2d" - reculer de 2 joursprices.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 |
Transformation de données avec Polars