polars.int_range#

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

Generate a range of integers.

Parameters:
start

Start of the range (inclusive). Defaults to 0.

end

End of the range (exclusive). If set to None (default), the value of start is used and start is set to 0.

step

Step size of the range.

dtype

Data type of the range.

eager

Evaluate immediately and return a Series. If set to False (default), return an expression instead.

Returns:
Expr or Series

Column of integer data type dtype.

See also

int_ranges

Generate a range of integers for each row of the input columns.

Examples

>>> pl.int_range(0, 3, eager=True)
shape: (3,)
Series: 'literal' [i64]
[
        0
        1
        2
]

end can be omitted for a shorter syntax.

>>> pl.int_range(3, eager=True)
shape: (3,)
Series: 'literal' [i64]
[
        0
        1
        2
]

Generate an index column by using int_range in conjunction with len().

>>> df = pl.DataFrame({"a": [1, 3, 5], "b": [2, 4, 6]})
>>> df.select(
...     pl.int_range(pl.len(), dtype=pl.UInt32).alias("index"),
...     pl.all(),
... )
shape: (3, 3)
┌───────┬─────┬─────┐
│ index ┆ a   ┆ b   │
│ ---   ┆ --- ┆ --- │
│ u32   ┆ i64 ┆ i64 │
╞═══════╪═════╪═════╡
│ 0     ┆ 1   ┆ 2   │
│ 1     ┆ 3   ┆ 4   │
│ 2     ┆ 5   ┆ 6   │
└───────┴─────┴─────┘