Skip to content

Take two sorted LazyFrames and merge them by the sorted key

Source code

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, ..., maintain_order = FALSE)

Arguments

other Other LazyFrame that must be merged.
key Key that is sorted.
These dots are for future extensions and must be empty.
maintain_order If TRUE, the output is guaranteed to have left-biased ordering for equal keys: rows from the left frame appear before rows from the right frame when their keys are equal.

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 │
#> └────────┴──────┘