polars.linear_spaces#
- polars.linear_spaces(
- start: NumericLiteral | TemporalLiteral | IntoExprColumn,
- end: NumericLiteral | TemporalLiteral | IntoExprColumn,
- num_samples: int | IntoExprColumn,
- *,
- closed: ClosedInterval = 'both',
- as_array: bool = False,
- eager: bool = False,
Generate a sequence of evenly-spaced values for each row between
start
andend
.The number of values in each sequence is determined by
num_samples
.- Parameters:
- start
Lower bound of the range.
- end
Upper bound of the range.
- num_samples
Number of samples in the output sequence.
- closed{‘both’, ‘left’, ‘right’, ‘none’}
Define which sides of the interval are closed (inclusive).
- as_array
Return result as a fixed-length
Array
.num_samples
must be a constant.- eager
Evaluate immediately and return a
Series
. If set toFalse
(default), return an expression instead.- .. warning::
This functionality is experimental. It may be changed at any point without it being considered a breaking change.
- Returns:
- Expr or Series
Column of data type
List(dtype)
.
See also
linear_space
Generate a single sequence of linearly-spaced values.
Examples
>>> df = pl.DataFrame({"start": [1, -1], "end": [3, 2], "num_samples": [4, 5]}) >>> df.with_columns(ls=pl.linear_spaces("start", "end", "num_samples")) shape: (2, 4) ┌───────┬─────┬─────────────┬────────────────────────┐ │ start ┆ end ┆ num_samples ┆ ls │ │ --- ┆ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 ┆ list[f64] │ ╞═══════╪═════╪═════════════╪════════════════════════╡ │ 1 ┆ 3 ┆ 4 ┆ [1.0, 1.666667, … 3.0] │ │ -1 ┆ 2 ┆ 5 ┆ [-1.0, -0.25, … 2.0] │ └───────┴─────┴─────────────┴────────────────────────┘ >>> df.with_columns(ls=pl.linear_spaces("start", "end", 3, as_array=True)) shape: (2, 4) ┌───────┬─────┬─────────────┬──────────────────┐ │ start ┆ end ┆ num_samples ┆ ls │ │ --- ┆ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 ┆ array[f64, 3] │ ╞═══════╪═════╪═════════════╪══════════════════╡ │ 1 ┆ 3 ┆ 4 ┆ [1.0, 2.0, 3.0] │ │ -1 ┆ 2 ┆ 5 ┆ [-1.0, 0.5, 2.0] │ └───────┴─────┴─────────────┴──────────────────┘