polars.Expr.dt.weekday#
- Expr.dt.weekday() Expr [source]#
Extract the week day from the underlying Date representation.
Applies to Date and Datetime columns.
Returns the ISO weekday number where monday = 1 and sunday = 7
- Returns:
- Expr
Expression of data type
UInt32
.
See also
Examples
>>> from datetime import timedelta, datetime >>> start = datetime(2001, 12, 22) >>> stop = datetime(2001, 12, 25) >>> df = pl.DataFrame( ... {"date": pl.date_range(start, stop, timedelta(days=1), eager=True)} ... ) >>> df shape: (4, 1) ┌─────────────────────┐ │ date │ │ --- │ │ datetime[μs] │ ╞═════════════════════╡ │ 2001-12-22 00:00:00 │ │ 2001-12-23 00:00:00 │ │ 2001-12-24 00:00:00 │ │ 2001-12-25 00:00:00 │ └─────────────────────┘ >>> df.with_columns( ... 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: (4, 4) ┌─────────────────────┬─────────┬──────────────┬─────────────┐ │ date ┆ weekday ┆ day_of_month ┆ day_of_year │ │ --- ┆ --- ┆ --- ┆ --- │ │ datetime[μs] ┆ u32 ┆ u32 ┆ u32 │ ╞═════════════════════╪═════════╪══════════════╪═════════════╡ │ 2001-12-22 00:00:00 ┆ 6 ┆ 22 ┆ 356 │ │ 2001-12-23 00:00:00 ┆ 7 ┆ 23 ┆ 357 │ │ 2001-12-24 00:00:00 ┆ 1 ┆ 24 ┆ 358 │ │ 2001-12-25 00:00:00 ┆ 2 ┆ 25 ┆ 359 │ └─────────────────────┴─────────┴──────────────┴─────────────┘