polars.cut#

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

Bin values into discrete values.

Warning

This function is experimental and might change without it being considered a breaking change.

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

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]   │
└──────┴─────────────┴──────────────┘