polars.Expr.map_batches#
- Expr.map_batches(
 - function: Callable[[Series], Series | Any],
 - return_dtype: PolarsDataType | None = None,
 - *,
 - agg_list: bool = False,
 Apply a custom python function to a whole Series or sequence of Series.
The output of this custom function must be a Series. If you want to apply a custom function elementwise over single values, see
map_elements(). A reasonable use case formapfunctions is transforming the values represented by an expression using a third-party library.Read more in the book.
- Parameters:
 - function
 Lambda/function to apply.
- return_dtype
 Dtype of the output Series.
- agg_list
 Aggregate list.
Warning
If
return_dtypeis not provided, this may lead to unexpected results. We allow this, but it is considered a bug in the user’s query.See also
Notes
If you are looking to map a function over a window function or group_by context, refer to func:
map_elementsinstead.Examples
>>> df = pl.DataFrame( ... { ... "sine": [0.0, 1.0, 0.0, -1.0], ... "cosine": [1.0, 0.0, -1.0, 0.0], ... } ... ) >>> df.select(pl.all().map_batches(lambda x: x.to_numpy().argmax())) shape: (1, 2) ┌──────┬────────┐ │ sine ┆ cosine │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞══════╪════════╡ │ 1 ┆ 0 │ └──────┴────────┘