Skip to content

Take two sorted LazyFrames and merge them by the sorted key

Description

The output of this operation will also be sorted. It is the callers responsibility that the frames are sorted by that key, otherwise the output will not make sense. The schemas of both LazyFrames must be equal.

Usage

<LazyFrame>$merge_sorted(other, key)

Arguments

other Other LazyFrame that must be merged.
key Key that is sorted.

Value

A polars LazyFrame

Examples

library("polars")

lf1 <- pl$LazyFrame(
  name = c("steve", "elise", "bob"),
  age = c(42, 44, 18)
)$sort("age")

lf2 <- pl$LazyFrame(
  name = c("anna", "megan", "steve", "thomas"),
  age = c(21, 33, 42, 20)
)$sort("age")

lf1$merge_sorted(lf2, key = "age")$collect()
#> shape: (7, 2)
#> ┌────────┬──────┐
#> │ name   ┆ age  │
#> │ ---    ┆ ---  │
#> │ str    ┆ f64  │
#> ╞════════╪══════╡
#> │ bob    ┆ 18.0 │
#> │ thomas ┆ 20.0 │
#> │ anna   ┆ 21.0 │
#> │ megan  ┆ 33.0 │
#> │ steve  ┆ 42.0 │
#> │ steve  ┆ 42.0 │
#> │ elise  ┆ 44.0 │
#> └────────┴──────┘