polars map-batches for dataframe
Map a custom Nushell closure over one or more dataframe columns.
Signature
> polars map-batches {flags} (closure) ...rest
Flags
--return-dtype, -d {any}: Data type to cast the closure's result to.--name, -n {string}: Name for the resulting column.
Parameters
closure: Closure to apply. Receives a list of single-column dataframes....rest: Names of columns to pass to the closure. If omitted, all columns are used.
Input/output types:
| input | output |
|---|---|
| polars_dataframe | polars_dataframe |
| polars_lazyframe | polars_dataframe |
Examples
Return a constant series from a closure
> [[a b]; [1 4] [2 5] [3 6]]
| polars into-df
| polars map-batches --name out { |_cols| [10 20 30] } a
╭───┬─────╮
│ # │ out │
├───┼─────┤
│ 0 │ 10 │
│ 1 │ 20 │
│ 2 │ 30 │
╰───┴─────╯Double the values of column a via a Nushell closure
> [[a b]; [1 4] [2 5] [3 6]]
| polars into-df
| polars map-batches { |cols| $cols | first | polars get a | each { |v| $v * 2 } } aSum two columns element-wise and rename the result
> [[a b]; [1 4] [2 5] [3 6]]
| polars into-df
| polars map-batches --name a_plus_b { |cols|
let a = $cols | get 0 | polars get a
let b = $cols | get 1 | polars get b
$a | zip $b | each { |pair| $pair.0 + $pair.1 }
} a b
╭───┬──────────╮
│ # │ a_plus_b │
├───┼──────────┤
│ 0 │ 5 │
│ 1 │ 7 │
│ 2 │ 9 │
╰───┴──────────╯Notes
The closure receives a list of single-column dataframes (one per named column) and must return a value that can be converted to a series — a single-column dataframe, a list, or a scalar. The result is returned as a single-column dataframe. The closure is invoked once with all columns at the time polars map-batches is run.