Skip to content
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

fix: output name of ternary expr #12381

Closed
wants to merge 3 commits into from

Conversation

nameexhaustion
Copy link
Collaborator

@nameexhaustion nameexhaustion commented Nov 11, 2023

Fixes issue #12380 (introduced by #12357).

The previous PR lost the series name and dtype for the non-matching branch, but they need to be preserved to have the correct output name and dtype even if the values are never used.

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Nov 11, 2023
@nameexhaustion nameexhaustion marked this pull request as ready for review November 11, 2023 03:42
@nameexhaustion nameexhaustion marked this pull request as draft November 11, 2023 03:48
@nameexhaustion nameexhaustion marked this pull request as ready for review November 11, 2023 04:23
truthy.len()
let len = truthy.len();
*falsy = match falsy.len() < len {
true => Series::full_null(falsy.name(), len, falsy.dtype()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we write this in terms of if else.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah

*truthy = falsy.clone();
falsy.len()
let len = falsy.len();
*truthy = match truthy.len() < len {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem

@nameexhaustion
Copy link
Collaborator Author

nameexhaustion commented Nov 11, 2023

@ritchie46 - just had a realization that it is probably not a good idea to be broadcasting to the matching branch... I think instead it's better to maintain the previous behavior (i.e. require all branches to have the same height and error otherwise) - otherwise we get:

df = pl.DataFrame(
    {"x": range(5), "y": range(5, 10)}, schema={"x": pl.Int8, "y": pl.Int64}
).with_columns(true=True, false=False)

df.select(pl.when(pl.first("true")).then(pl.col("x")))
# shape: (5, 1)
# ┌─────┐
# │ x   │
# │ --- │
# │ i8  │
# ╞═════╡
# │ 0   │
# │ 1   │
# │ 2   │
# │ 3   │
# │ 4   │
# └─────┘
df.select(pl.when(pl.first("false")).then(pl.col("x")))
# we get this if we broadcast to the matching branch (on 0.19.13):
# >>> df.select(pl.when(pl.first("false")).then(pl.col("x")))
# shape: (1, 1)
# ┌──────┐
# │ x    │
# │ ---  │
# │ i8   │
# ╞══════╡
# │ null │
# └──────┘

# but it should probably instead be this (on 0.19.12):
# shape: (5, 1)
# ┌──────┐
# │ x    │
# │ ---  │
# │ i8   │
# ╞══════╡
# │ null │
# │ null │
# │ null │
# │ null │
# │ null │
# └──────┘

So I think instead it's probably better to have #12357 to be reverted instead of merging this (or I can adjust this PR as well to remove the special broadcasting) - @JulianCologne raises a good point in #12363, a better fix for the original issue (#12354) is probably to have .first / .last always return a 1-item Series instead.

*as a note, the current change in behavior in 0.19.13 might be quite a serious issue

@ritchie46
Copy link
Member

Yes, let's revert this and fix first last.

@ritchie46 ritchie46 closed this Nov 12, 2023
@nameexhaustion nameexhaustion deleted the ternary-name branch February 22, 2024 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants