polars.Expr.arr.agg#

Expr.arr.agg(expr: Expr) Expr[source]#

Run any polars aggregation expression against the arrays’ elements.

Parameters:
expr

Expression to run. Note that you can select an element with pl.element().

See also

polars.Expr.arr.eval

Evaluate any expression without automatic explode.

polars.Expr.list.agg

Same for the List datatype.

Examples

>>> df = pl.Series(
...     "a", [[1, None], [42, 13], [None, None]], pl.Array(pl.Int64, 2)
... ).to_frame()
>>> df.with_columns(null_count=pl.col.a.arr.agg(pl.element().null_count()))
shape: (3, 2)
┌───────────────┬────────────┐
│ a             ┆ null_count │
│ ---           ┆ ---        │
│ array[i64, 2] ┆ u32        │
╞═══════════════╪════════════╡
│ [1, null]     ┆ 1          │
│ [42, 13]      ┆ 0          │
│ [null, null]  ┆ 2          │
└───────────────┴────────────┘
>>> df.with_columns(no_nulls=pl.col.a.arr.agg(pl.element().drop_nulls()))
shape: (3, 2)
┌───────────────┬───────────┐
│ a             ┆ no_nulls  │
│ ---           ┆ ---       │
│ array[i64, 2] ┆ list[i64] │
╞═══════════════╪═══════════╡
│ [1, null]     ┆ [1]       │
│ [42, 13]      ┆ [42, 13]  │
│ [null, null]  ┆ []        │
└───────────────┴───────────┘