Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Polars · Module

.dt.month

Extracts the calendar month from Date or Datetime expressions as one-based Int8 values from 1 through 12.

How it works

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.

Syntax

Expr.dt.month() → Expr

Returns

Expr of data type Int8 containing month numbers from 1 to 12.

Key points

  • The method derives the calendar month from each temporal value.
  • Month numbering begins at one rather than zero.
  • Every non-null result belongs to the inclusive range 1–12.
  • It is an expression method and returns another Polars expression.
  • The resulting expression uses the compact Int8 integer type.

Common mistakes

  • Use this temporal accessor with Date or Datetime columns; it is not documented for other data types.

Example: Extract calendar months

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.

When to use it

Calendar-month grouping

Create a numeric month column for reporting or grouping.

Feature engineering

Derive a compact temporal feature for downstream expressions.

DataFrame enrichment

Project month values while transforming a DataFrame.

Interview questions

QWhat does 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.

QIs the extracted month zero-based or one-based?

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.

QHow is this method typically used in a Polars pipeline?

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.month
  • Expr.dt.to_string
  • Date
  • Datetime

Source: polars.Expr.dt.month — Polars documentation. Checked 2026-10-15.