bulk_append

bulk_append(*exprs)

Combines multiple Polars expressions using pl.Expr.append() internally.

You may consider rechunking the result of bulk_append() using pl.Expr.rechunk() for better performance.

Parameters

exprs : pl.Expr | Iterable[pl.Expr] = ()

One or more pl.Expr objects passed as separate arguments, or a single iterable containing multiple pl.Expr objects.

Returns

: pl.Expr

A single Polars expression resulting from appending all input expressions.

Examples

DataFrame Context

Because bulk_append() may change the total number of rows, use it with caution inside with_columns().

Append the last value to the first:

import polars as pl
import turtle_island as ti

df = pl.DataFrame(
    {"a": [1, 2, 3], "b": ["x", "y", "z"], "c": [4.4, 5.5, 6.6]}
)
df.select(ti.bulk_append(pl.all().first(), pl.all().last()))
shape: (2, 3)
abc
i64strf64
1"x"4.4
3"z"6.6

List Namespace Context

In the list namespace, it may be easier to think of each row as an element in a list. Conceptually, you’re working with a pl.Series, where each row corresponds to one item in the list.

A similar operation applies to lists, where the last element is appended to the first.

df2 = pl.DataFrame(
    {
        "x": [[1, 2, 3, 4], [5, 6, 7, 8]],
        "y": [[9, 10, 11, 12], [13, 14, 15, 16]],
    }
)
df2.select(
    pl.all().list.eval(
        ti.bulk_append(pl.element().first(), pl.element().last())
    )
)
shape: (2, 2)
xy
list[i64]list[i64]
[1, 4][9, 12]
[5, 8][13, 16]