make_index

make_index(offset=0, *, name='index')

Returns a Polars expression that creates a virtual row index.

Borrowed from the Polars documentation and adapted for expression-level use.

Unlike pl.DataFrame.with_row_index(), which works at the DataFrame level, this expression can be composed inline and reused without materializing an actual column.

Parameters

offset : int = 0

Start the index at this offset. Cannot be negative.

name : str = 'index'

The name of the resulting column.

Returns

: pl.Expr

A Polars expression that yields a sequential integer index starting from 0.

Examples

DataFrame Context

Adds a sequential index column to the DataFrame:

import polars as pl
import turtle_island as ti

df = pl.DataFrame({"a": [1, 3, 5], "b": [2, 4, 6]})
df.select(ti.make_index(), pl.all())
shape: (3, 3)
indexab
u32i64i64
012
134
256