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

Polars · Function

pl.int_range

Generates an integer range with an inclusive start and exclusive end. It returns an expression by default or an immediately evaluated Series when eager=True.

How it works

int_range builds integer values from an inclusive start toward an exclusive end using the requested step. By default it returns a lazy expression; setting eager=True evaluates it immediately as a Series, and omitting end treats the supplied start as the exclusive endpoint from zero.

Syntax

polars.int_range(start: int | IntoExprColumn = 0, end: int | IntoExprColumn | None = None, step: int = 1, *, dtype: PolarsIntegerType | DataTypeExpr = Int64, eager: bool = False) → Expr | Series

Parameters

NameTypeDescription
startint | IntoExprColumnStart of the range, inclusive; defaults to 0.
endint | IntoExprColumn | NoneExclusive endpoint. If omitted, start is used as the endpoint and the actual start becomes 0.
stepintStep size of the range.
dtypePolarsIntegerType | DataTypeExprInteger data type for the output.
eagerboolIf true, evaluate immediately and return a Series; otherwise return an expression.

Returns

Expr or SeriesColumn of integer data type dtype.

Key points

  • Use this function when a single integer sequence is needed.
  • The default starting value is zero, and an explicitly supplied start is included.
  • step controls the increment between generated values.
  • The dtype argument selects the integer type of the output.
  • For a separate range per input row, the documentation points to int_ranges instead.

Common mistakes

  • The endpoint is excluded. When end is omitted, the single positional value becomes the exclusive endpoint and the range starts at zero.
  • The default result is an expression, not an evaluated Series; use eager=True when immediate materialization is required.

Example: Create an index column

import polars as pl

df = pl.DataFrame({"value": [10, 20, 30]})
result = df.select(
    pl.int_range(pl.len(), dtype=pl.UInt32).alias("index"),
    pl.all(),
)
print(result)

Creates a zero-based UInt32 index expression whose length matches the input DataFrame.

When to use it

Add an index column

Create sequential row labels during a DataFrame select.

Build an eager integer Series

Produce a materialized sequence for direct Series use.

Control sequence spacing and type

Generate values with a custom increment or integer dtype.

Interview questions

QWhat does pl.int_range generate, and what are its endpoint and dtype defaults?

It generates an integer range with an inclusive start and exclusive end. The default step is 1, and the default dtype is Int64.

Follow-up: How would you create a DataFrame index? Use pl.int_range(pl.len(), dtype=pl.UInt32) in a select expression and give it an alias.

QHow does the eager argument change the return type?

With eager=False, the function returns an expression that can participate in a DataFrame query. With eager=True, it evaluates immediately and returns a Series.

Follow-up: Why might the expression form be preferable? It can be composed with select and other expressions before execution.

QWhat does omitting end do?

The one-argument form uses the supplied value as end, sets start to zero, and therefore produces values from zero up to but not including that value. For example, pl.int_range(3, eager=True) yields 0, 1, and 2.

Follow-up: How can the output type be narrowed for an index? Pass an integer dtype such as pl.UInt32 through dtype.

  • int_ranges

Source: polars.int_range — Polars documentation. Checked 2026-10-15.