polars.cut#

polars.cut(s: Series, bins: list[float], labels: list[str] | None = None, break_point_label: str = 'break_point', category_label: str = 'category') DataFrame[source]#

Bin values into discrete values.

Deprecated since version 0.16.8: pl.cut(series, …) has been deprecated; use series.cut(…)

Parameters:
s

Series to bin.

bins

Bins to create.

labels

Labels to assign to the bins. If given the length of labels must be len(bins) + 1.

break_point_label

Name given to the breakpoint column.

category_label

Name given to the category column.

Returns:
DataFrame

Warning

This functionality is experimental and may change without it being considered a breaking change.

Examples

>>> a = pl.Series("a", [v / 10 for v in range(-30, 30, 5)])
>>> pl.cut(a, bins=[-1, 1])
shape: (12, 3)
┌──────┬─────────────┬──────────────┐
│ a    ┆ break_point ┆ category     │
│ ---  ┆ ---         ┆ ---          │
│ f64  ┆ f64         ┆ cat          │
╞══════╪═════════════╪══════════════╡
│ -3.0 ┆ -1.0        ┆ (-inf, -1.0] │
│ -2.5 ┆ -1.0        ┆ (-inf, -1.0] │
│ -2.0 ┆ -1.0        ┆ (-inf, -1.0] │
│ -1.5 ┆ -1.0        ┆ (-inf, -1.0] │
│ …    ┆ …           ┆ …            │
│ 1.0  ┆ 1.0         ┆ (-1.0, 1.0]  │
│ 1.5  ┆ inf         ┆ (1.0, inf]   │
│ 2.0  ┆ inf         ┆ (1.0, inf]   │
│ 2.5  ┆ inf         ┆ (1.0, inf]   │
└──────┴─────────────┴──────────────┘