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

Expr.first, Expr.last should always produce same size output (PanicException: The column lengths in the DataFrame are not equal) #12363

Closed
2 tasks done
Julian-J-S opened this issue Nov 10, 2023 · 4 comments · Fixed by #12401
Assignees
Labels
bug Something isn't working python Related to Python Polars

Comments

@Julian-J-S
Copy link
Contributor

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

df = pl.DataFrame(
    {
        "a": [1, 2],
        "b": [None, None],
    }
)

df.select(
    pl.all().drop_nulls().sum().name.suffix("_sum"),
    pl.all().drop_nulls().std().name.suffix("_std"),
    # pl.all().drop_nulls().first().name.suffix("_first"), # PanicException: The column lengths in the DataFrame are not equal.
    pl.all().drop_nulls().first().len().name.suffix("_len"),
)

shape: (1, 6)
┌───────┬───────┬──────────┬───────┬───────┬───────┐
│ a_sumb_suma_stdb_stda_lenb_len │
│ ------------------   │
│ i64f32f64f32u32u32   │
╞═══════╪═══════╪══════════╪═══════╪═══════╪═══════╡
│ 30.00.707107null10<<<<<<<<<<<<<< should not be of size 0!
└───────┴───────┴──────────┴───────┴───────┴───────┘

Log output

No response

Issue description

first / last should behave like other aggreation functions and return a consistent size output (1 element) even if the column is empty!

Empty column behaviour examples:

BUT

  • first -> empy zero length df but should be null

Those inconsistent 0-size results can cause multiple problems.
A recent one (#12354) was already fixed (#12357) by adding extra logic to when/then but the real problem was actually first returning a 0-size result. By fixing the return value of first no extrac logic is required.

Expected behavior

first / last should behave like other aggreation functions and return a consistent size output (1 element) even if the column is empty!

  • correct: [].first -> null
  • wrong: [].first -> 0-size df

Installed versions

0.19.12
@Julian-J-S Julian-J-S added bug Something isn't working python Related to Python Polars labels Nov 10, 2023
@reswqa
Copy link
Collaborator

reswqa commented Nov 10, 2023

I see your point. My only concern is that this could easily make it seem like the first element of this column is Null instead of empty. After all, first and last have such meanings.

@Julian-J-S
Copy link
Contributor Author

I see your point. My only concern is that this could easily make it seem like the first element of this column is Null instead of empty. After all, first and last have such meanings.

good point!

However, I think being consistent and always returning a value is better for the user.
This "problem" can lead to bugs/panics/crashes that the user cannot control.

The ambiguity is not allways avoidable but also consistent with the other functions.

  • [].std = null
  • [null, null].std = null

What the user can do is drop_nulls() before and when the result is null you know it is empty

@reswqa
Copy link
Collaborator

reswqa commented Nov 10, 2023

I think there are some differences between first/last and std/mean etc. It clearly indicates the element at the specific position, but the others do not.

But I truly understand the benefits of being consistent with other agg funcs. So I don't have a strong tendency. :)

@nameexhaustion
Copy link
Collaborator

It's reasonable - DuckDB notably does this:

>>> df = pl.DataFrame({"x": []})
>>> df.select(pl.first("x").alias("pl.first(x)"), pl.last("x").alias("pl.last(x)"))
shape: (0, 2)
┌─────────────┬────────────┐
│ pl.first(x) ┆ pl.last(x) │
│ ------        │
│ f32f32        │
╞═════════════╪════════════╡
└─────────────┴────────────┘
>>> duckdb.sql("select first(x), last(x) from df")
┌──────────┬─────────┐
│ first(x) │ last(x) │
│  floatfloat  │
├──────────┼─────────┤
│     NULLNULL │
└──────────┴─────────┘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working python Related to Python Polars
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants