Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
  • Categories

    • Bits
    • Bytes
    • Chart
    • Conversions
    • Core
    • Database
    • Dataframe
    • Dataframe Or Lazyframe
    • Date
    • Debug
    • Default
    • Env
    • Experimental
    • Expression
    • Filesystem
    • Filters
    • Formats
    • Generators
    • Hash
    • History
    • Lazyframe
    • Math
    • Misc
    • Network
    • Path
    • Platform
    • Plugin
    • Prompt
    • Random
    • Removed
    • Shells
    • Strings
    • System
    • Viewers

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:

inputoutput
polars_dataframepolars_dataframe
polars_lazyframepolars_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 } } a

Sum 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.