Dates and times in Python
The dt namespace does the temporal work, on real Date and Datetime types.
import polars as pl
frame = pl.DataFrame({"day": ["2026-07-27", "2026-07-29", "2026-08-02"]})
dated = frame.with_columns(
pl.col("day").str.to_date().alias("date"),
).with_columns(
pl.col("date").dt.weekday().alias("weekday"),
pl.col("date").dt.strftime("%b %d").alias("label"),
(pl.col("date") + pl.duration(days=7)).alias("next_week"),
)
print(dated)
print(dated.filter(pl.col("date").dt.month() == 8).height)
How it works
str.to_dateparses text into a Date column.dt.weekday,dt.monthand friends extract parts.- Date arithmetic uses durations, not raw integers.
Keywords and builtins used here
asprint
The run, in numbers
- Lines
- 13
- Characters to type
- 413
- Tokens
- 169
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 92 seconds.
Step 1 of 2 in Time series, step 23 of 32 in Polars.