Skip to content

Run any polars aggregation expression against the lists’ elements

Source code

Description

This looks similar to $list$eval(), but the key difference is that $list$agg() automatically explodes the list if the expression inside returns a scalar (while $list$eval() always returns a list).

Usage

<Expr>$list$agg(expr)

Arguments

expr Expression to run. Note that you can select an element with pl$element(), pl$first(), and more. See Examples.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(a = list(c(1, NA), c(42, 13), c(NA, NA)))

# The column "null_count" has dtype u32 because `$null_count()` returns a
# scalar for each sub-list. Using `$list$eval()` instead would return a
# column with dtype list(u32).
df$with_columns(
  null_count = pl$col("a")$list$agg(pl$element()$null_count())
)
#> shape: (3, 2)
#> ┌──────────────┬────────────┐
#> │ a            ┆ null_count │
#> │ ---          ┆ ---        │
#> │ list[f64]    ┆ u32        │
#> ╞══════════════╪════════════╡
#> │ [1.0, null]  ┆ 1          │
#> │ [42.0, 13.0] ┆ 0          │
#> │ [null, null] ┆ 2          │
#> └──────────────┴────────────┘
# The column "no_nulls" has dtype list(u32) because the expression doesn't
# guarantee to return a scalar.
df$with_columns(
  no_nulls = pl$col("a")$list$agg(pl$element()$drop_nulls())
)
#> shape: (3, 2)
#> ┌──────────────┬──────────────┐
#> │ a            ┆ no_nulls     │
#> │ ---          ┆ ---          │
#> │ list[f64]    ┆ list[f64]    │
#> ╞══════════════╪══════════════╡
#> │ [1.0, null]  ┆ [1.0]        │
#> │ [42.0, 13.0] ┆ [42.0, 13.0] │
#> │ [null, null] ┆ []           │
#> └──────────────┴──────────────┘