Calendar-month grouping
Create a numeric month column for reporting or grouping.
Polars · Module
Extracts the calendar month from Date or Datetime expressions as one-based Int8 values from 1 through 12.
Call .dt.month() on a date or datetime expression to produce the calendar month for every value. The resulting expression has Int8 type and uses one-based numbering, so January is 1 and December is 12.
Expr.dt.month() → ExprExpr of data type Int8 containing month numbers from 1 to 12.
import polars as pl
from datetime import date
df = pl.DataFrame({"date": [date(2001, 1, 1), date(2001, 6, 30), date(2001, 12, 27)]})
result = df.with_columns(pl.col("date").dt.month().alias("month"))
print(result)This adds an Int8 expression containing the calendar month for each date.
Create a numeric month column for reporting or grouping.
Derive a compact temporal feature for downstream expressions.
Project month values while transforming a DataFrame.
Expr.dt.month() return?Expr.dt.month() extracts the month from each Date or Datetime value. It returns an expression with Int8 data type and values from 1 through 12.
Follow-up: How would you obtain month names instead? Use a temporal string-formatting method with an appropriate strftime month-name specifier.
The output is one-based: January produces 1, and December produces 12. This makes the result suitable for ordinary calendar-month grouping without adding an offset.
Follow-up: How would you validate the output domain? Check that the resulting integer values remain within the documented inclusive range 1 to 12.
Use it inside a DataFrame expression, such as pl.col("date").dt.month().alias("month"), and pass that expression to with_columns or another expression-consuming operation. The result is a derived column containing month numbers.
Follow-up: How could the derived month be used analytically? Group or aggregate by the resulting month column after creating it.
Series.dt.monthExpr.dt.to_stringDateDatetime