Skip to content

polars/polars-join-without-validate

Pack Severity Fix Versions
Polars info none, edit by hand All versions

A Polars join allows duplicate keys on either side by default. Pass validate when the keys should be unique on one or both sides.

df.join(df, on="x")
df.join(df, on="x", validate="1:1")

The rule reports join with on=, left_on= or right_on= and no validate=, unless the lines before it prove that the join cannot repeat a row. A left join needs the right key proven unique, a right join needs the left key, and every other join needs both. group_by("x") with an aggregation and unique(subset="x") prove a key unique, and so does a check that stops on a duplicate.

The rule leaves a join with how="cross", how="semi" or how="anti" alone.

Two identical keys on each side yielded 4 rows. 1:1 validation raised ComputeError.

Edit the code using the replacement in the example.

Polars validation is cheap. In one measurement on 500,000 to 5,000,000 rows with Polars 2.2.0, a join with validate="m:1" took 0.9 to 1.1 times as long as the join alone. The validation ignores null keys, so two rows with a null key pass validate="1:1".

A Polars join allows duplicate keys on either side by default. Pass validate when the keys should be unique on one or both sides.

See all rules in the Polars pack.