polars.LazyFrame.join

LazyFrame.join(ldf: polars.internals.lazy_frame.LazyFrame, left_on: Optional[Union[str, polars.internals.expr.Expr, List[Union[str, polars.internals.expr.Expr]]]] = None, right_on: Optional[Union[str, polars.internals.expr.Expr, List[Union[str, polars.internals.expr.Expr]]]] = None, on: Optional[Union[str, polars.internals.expr.Expr, List[Union[str, polars.internals.expr.Expr]]]] = None, how: str = 'inner', suffix: str = '_right', allow_parallel: bool = True, force_parallel: bool = False, asof_by: Optional[Union[str, List[str]]] = None, asof_by_left: Optional[Union[str, List[str]]] = None, asof_by_right: Optional[Union[str, List[str]]] = None) polars.internals.lazy_frame.LDF

Add a join operation to the Logical Plan.

Parameters
ldf

Lazy DataFrame to join with.

left_on

Join column of the left DataFrame.

right_on

Join column of the right DataFrame.

on

Join column of both DataFrames. If set, left_on and right_on should be None.

how
one of:

“inner” “left” “outer” “asof”, “cross” “semi” “anti”

suffix

Suffix to append to columns with a duplicate name.

allow_parallel

Allow the physical plan to optionally evaluate the computation of both DataFrames up to the join in parallel.

force_parallel

Force the physical plan to evaluate the computation of both DataFrames up to the join in parallel.

asof_by

join on these columns before doing asof join

asof_by_left

join on these columns before doing asof join

asof_by_right

join on these columns before doing asof join

# Asof joins
This is similar to a left-join except that we match on nearest key rather than equal keys.
The keys must be sorted to perform an asof join

Examples

>>> df = pl.DataFrame(
...     {
...         "foo": [1, 2, 3],
...         "bar": [6.0, 7.0, 8.0],
...         "ham": ["a", "b", "c"],
...     }
... ).lazy()
>>> other_df = pl.DataFrame(
...     {
...         "apple": ["x", "y", "z"],
...         "ham": ["a", "b", "d"],
...     }
... ).lazy()
>>> df.join(other_df, on="ham").collect()
shape: (2, 4)
┌─────┬─────┬─────┬───────┐
│ foo ┆ bar ┆ ham ┆ apple │
│ --- ┆ --- ┆ --- ┆ ---   │
│ i64 ┆ f64 ┆ str ┆ str   │
╞═════╪═════╪═════╪═══════╡
│ 1   ┆ 6.0 ┆ a   ┆ x     │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 2   ┆ 7.0 ┆ b   ┆ y     │
└─────┴─────┴─────┴───────┘
>>> df.join(other_df, on="ham", how="outer").collect()
shape: (4, 4)
┌──────┬──────┬─────┬───────┐
│ foo  ┆ bar  ┆ ham ┆ apple │
│ ---  ┆ ---  ┆ --- ┆ ---   │
│ i64  ┆ f64  ┆ str ┆ str   │
╞══════╪══════╪═════╪═══════╡
│ 1    ┆ 6.0  ┆ a   ┆ x     │
├╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 2    ┆ 7.0  ┆ b   ┆ y     │
├╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ null ┆ null ┆ d   ┆ z     │
├╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ 3    ┆ 8.0  ┆ c   ┆ null  │
└──────┴──────┴─────┴───────┘