polars.internals.expr.datetime.ExprDateTimeNameSpace.weekday#

ExprDateTimeNameSpace.weekday() Expr[source]#

Extract the week day from the underlying Date representation.

Applies to Date and Datetime columns.

Returns the weekday number where monday = 0 and sunday = 6

Returns:
Week day as UInt32

Examples

>>> from datetime import timedelta, datetime
>>> start = datetime(2001, 1, 1)
>>> stop = datetime(2001, 1, 9)
>>> df = pl.DataFrame({"date": pl.date_range(start, stop, timedelta(days=3))})
>>> df
shape: (3, 1)
┌─────────────────────┐
│ date                │
│ ---                 │
│ datetime[μs]        │
╞═════════════════════╡
│ 2001-01-01 00:00:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2001-01-04 00:00:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2001-01-07 00:00:00 │
└─────────────────────┘
>>> df.select(
...     [
...         pl.col("date").dt.weekday().alias("weekday"),
...         pl.col("date").dt.day().alias("day_of_month"),
...         pl.col("date").dt.ordinal_day().alias("day_of_year"),
...     ]
... )
shape: (3, 3)
┌─────────┬──────────────┬─────────────┐
│ weekday ┆ day_of_month ┆ day_of_year │
│ ---     ┆ ---          ┆ ---         │
│ u32     ┆ u32          ┆ u32         │
╞═════════╪══════════════╪═════════════╡
│ 0       ┆ 1            ┆ 1           │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 3       ┆ 4            ┆ 4           │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 6       ┆ 7            ┆ 7           │
└─────────┴──────────────┴─────────────┘