Skip to content

Add an external context to the computation graph

Source code

Description

This allows expressions to also access columns from DataFrames or LazyFrames that are not part of this one.

Usage

<LazyFrame>$with_context(other)

Arguments

other Data/LazyFrame to have access to. This can be a list of DataFrames and LazyFrames.

Value

A LazyFrame

Examples

library(polars)

lf = pl$LazyFrame(a = c(1, 2, 3), b = c("a", "c", NA))
lf_other = pl$LazyFrame(c = c("foo", "ham"))

lf$with_context(lf_other)$select(
  pl$col("b") + pl$col("c")$first()
)$collect()
#> shape: (3, 1)
#> ┌──────┐
#> │ b    │
#> │ ---  │
#> │ str  │
#> ╞══════╡
#> │ afoo │
#> │ cfoo │
#> │ null │
#> └──────┘
# Fill nulls with the median from another lazyframe:
train_lf = pl$LazyFrame(
  feature_0 = c(-1.0, 0, 1), feature_1 = c(-1.0, 0, 1)
)
test_lf = pl$LazyFrame(
  feature_0 = c(-1.0, NA, 1), feature_1 = c(-1.0, 0, 1)
)

test_lf$with_context(train_lf$select(pl$all()$name$suffix("_train")))$select(
  pl$col("feature_0")$fill_null(pl$col("feature_0_train")$median())
)$collect()
#> shape: (3, 1)
#> ┌───────────┐
#> │ feature_0 │
#> │ ---       │
#> │ f64       │
#> ╞═══════════╡
#> │ -1.0      │
#> │ 0.0       │
#> │ 1.0       │
#> └───────────┘