-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to select right table join key from join output #13130
Comments
The right key is redundant they will always be the same so polars doesn't create it. Select "x". |
Is it? Joins in other systems don't work that way and I think that's only true when the join is an inner join and the predicates are all equality predicates. Here's DuckDB:
|
This functionality was also asked about on SO earlier: https://stackoverflow.com/questions/77684593/ User wanted df = pl.DataFrame({
"foo": [1, 2, 3],
"bar": [6.0, 7.0, 8.0],
"ham": ["a", "b", "c"],
})
other_df = pl.DataFrame({
"apple": ["x", None, "z"],
"ham": ["a", "b", "d"],
})
df.join(other_df.with_columns(pl.col("ham").name.suffix("_right")), on="ham", how="left")
# shape: (3, 5)
# ┌─────┬─────┬─────┬───────┬───────────┐
# │ foo ┆ bar ┆ ham ┆ apple ┆ ham_right │
# │ --- ┆ --- ┆ --- ┆ --- ┆ --- │
# │ i64 ┆ f64 ┆ str ┆ str ┆ str │
# ╞═════╪═════╪═════╪═══════╪═══════════╡
# │ 1 ┆ 6.0 ┆ a ┆ x ┆ a │
# │ 2 ┆ 7.0 ┆ b ┆ null ┆ b │
# │ 3 ┆ 8.0 ┆ c ┆ null ┆ null │
# └─────┴─────┴─────┴───────┴───────────┘ |
Oh, right. I thought about inner join. My bad. |
Yeap, good point. We changed this behavior for outer joins. Might need to do that for left as well. |
join_asof too please. |
If someone is diving in to determine which columns are returned, #9621 is another good one to address. Currently when you join on an expression, you may or may not end up with an unexpected |
@cpcloud for the time being, left joins will always either return the left-column's value or a import polars as pl
from polars import col, when
left = pl.DataFrame({"x": [1, 2, 3, 4]})
right = pl.DataFrame({"x": [3, 2, 1]})
def join_with_right_columns(left, right, on, *args, **kwargs):
return left.join(right.with_columns(join_success=pl.lit(True)), on=on, *args, **kwargs).with_columns(
when(col("join_success").is_not_null()).then(x).alias(f"{x}_right")
for x in on
).drop("join_success")
out = join_with_right_columns(left, right, on="x", how="left")
print(out)
|
Hi Phillip just copy the the join column :)
|
Wouldn't it be simpler to just do |
Closing in favor of #13441 |
@stinodego Did you mean to link a different issue? |
Yes, updated. We will implement a left_coalesce join and the default join will be as you request it. |
Checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of Polars.
Reproducible example
Log output
No response
Issue description
I would like to be able to operate on columns coming from the right side of a join, without having to rename them beforehand.
Expected behavior
I would expect there to be some way to select the right side join key without having to rename.
Installed versions
The text was updated successfully, but these errors were encountered: