polars.Expr.map#

Expr.map(f: Callable[[Series], Union[Series, Any]], return_dtype: Optional[Union[Type[DataType], DataType]] = None, agg_list: bool = False) Expr[source]#

Apply a custom python function to a 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 apply(). A use case for map is when you want to transform an expression with a third-party library.

Read more in the book.

Parameters:
f

Lambda/ function to apply.

return_dtype

Dtype of the output Series.

agg_list

Aggregate list

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(lambda x: x.to_numpy().argmax())))
shape: (1, 2)
┌──────┬────────┐
│ sine ┆ cosine │
│ ---  ┆ ---    │
│ i64  ┆ i64    │
╞══════╪════════╡
│ 1    ┆ 0      │
└──────┴────────┘