polars.apply#

polars.apply(exprs: Sequence[str | polars.internals.expr.expr.Expr], f: Callable[[Sequence[Series]], Union[Series, Any]], return_dtype: Optional[type[polars.datatypes.DataType]] = None) Expr[source]#

Apply a custom/user-defined function (UDF) in a GroupBy context.

Depending on the context it has the following behavior:

  • Select

    Don’t use apply, use map

  • GroupBy

    expected type f: Callable[[Series], Series] Applies a python function over each group.

Parameters:
exprs

Input Series to f

f

Function to apply over the input

return_dtype

dtype of the output Series

Returns:
Expr