polars.Expr.dt.week#

Expr.dt.week() Expr[source]#

Extract the week from the underlying Date representation.

Applies to Date and Datetime columns.

Returns the ISO week number starting from 1. The return value ranges from 1 to 53. (The last week of year differs by years.)

Returns:
Expr

Expression of data type UInt32.

Examples

>>> from datetime import timedelta, datetime
>>> start = datetime(2001, 1, 1)
>>> stop = datetime(2001, 4, 1)
>>> df = pl.DataFrame(
...     {"date": pl.date_range(start, stop, timedelta(days=31), eager=True)}
... )
>>> df
shape: (3, 1)
┌─────────────────────┐
│ date                │
│ ---                 │
│ datetime[μs]        │
╞═════════════════════╡
│ 2001-01-01 00:00:00 │
│ 2001-02-01 00:00:00 │
│ 2001-03-04 00:00:00 │
└─────────────────────┘
>>> df.select(pl.col("date").dt.week())
shape: (3, 1)
┌──────┐
│ date │
│ ---  │
│ u32  │
╞══════╡
│ 1    │
│ 5    │
│ 9    │
└──────┘