typestar

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

  1. str.to_date parses text into a Date column.
  2. dt.weekday, dt.month and friends extract parts.
  3. Date arithmetic uses durations, not raw integers.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 2 in Time series, step 23 of 32 in Polars.

← Previous Next →

Dates and times in other languages