Calendar-based aggregation
Add a numeric weekday column for grouping or reporting.
Polars · Module
Extracts the ISO weekday number from Date or Datetime expressions. Monday is 1 and Sunday is 7, and the result has Int8 data type.
Call dt.weekday() on a date- or datetime-valued expression to produce the ISO weekday number for each value. The result is an Int8 expression, with Monday represented by 1 and Sunday by 7; it can be added to a DataFrame using with_columns.
Expr.dt.weekday() → ExprExpr of data type Int8 containing the ISO weekday number for each Date or Datetime value.
import polars as pl
from datetime import date
df = pl.DataFrame({"date": [date(2024, 1, 1), date(2024, 1, 7)]})
result = df.with_columns(
pl.col("date").dt.weekday().alias("weekday")
)
print(result)Adds an ISO weekday-number column: Monday is 1 and Sunday is 7.
Add a numeric weekday column for grouping or reporting.
Filter records to weekdays or weekends.
Build weekday features for analytics or machine-learning pipelines.
Expr.dt.weekday() return?Expr.dt.weekday() extracts the ISO weekday from a Date or Datetime expression. It returns an Expr whose data type is Int8, using 1 for Monday through 7 for Sunday.
Follow-up: How would you retain the original date while adding the weekday? Use with_columns(pl.col("date").dt.weekday().alias("weekday")).
The output follows ISO weekday numbering: Monday is 1 and Sunday is 7. This makes weekday comparisons independent of locale-specific Sunday-first numbering.
Follow-up: How would you select weekend rows? Filter where the derived weekday is greater than 5, such as pl.col("date").dt.weekday() > 5.
It is an expression method, so it is evaluated as part of a Polars expression pipeline rather than immediately extracting one scalar. It can therefore be used in transformations such as with_columns and filter.
Follow-up: What output type should downstream code expect? An Int8 expression, not a string containing a weekday name.
Expr.dt.dayExpr.dt.weekExpr.dt.ordinal_daySeries.dt.weekday