polars.Expr.clip#

Expr.clip(lower_bound: int | float, upper_bound: int | float) Self[source]#

Clip (limit) the values in an array to a min and max boundary.

Only works for numerical types.

If you want to clip other dtypes, consider writing a “when, then, otherwise” expression. See when() for more information.

Parameters:
lower_bound

Lower bound.

upper_bound

Upper bound.

Examples

>>> df = pl.DataFrame({"foo": [-50, 5, None, 50]})
>>> df.with_columns(pl.col("foo").clip(1, 10).alias("foo_clipped"))
shape: (4, 2)
┌──────┬─────────────┐
│ foo  ┆ foo_clipped │
│ ---  ┆ ---         │
│ i64  ┆ i64         │
╞══════╪═════════════╡
│ -50  ┆ 1           │
│ 5    ┆ 5           │
│ null ┆ null        │
│ 50   ┆ 10          │
└──────┴─────────────┘