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

feat: Add list.pad_start() #20674

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open

Conversation

etiennebacher
Copy link
Contributor

@etiennebacher etiennebacher commented Jan 12, 2025

Fixes #10283

Currently, this allows to do this:

import polars as pl

(
  pl.DataFrame(
    {
      "a": [[1], [], [1, 2, 3]], 
      "int": [0, 999, 2], 
      "float": [0.0, 999, 2]
    }
  )
  .select(
    filled_int=pl.col("a").list.pad_start(fill_value=pl.col("int")),
    filled_float=pl.col("a").list.pad_start(fill_value=pl.col("float"))
  )
)

shape: (3, 2)
┌─────────────────┬───────────────────────┐
│ filled_intfilled_float          │
│ ------                   │
│ list[i64]       ┆ list[f64]             │
╞═════════════════╪═══════════════════════╡
│ [0, 0, 1]       ┆ [0.0, 0.0, 1.0]       │
│ [999, 999, 999] ┆ [999.0, 999.0, 999.0] │
│ [1, 2, 3]       ┆ [1.0, 2.0, 3.0]       │
└─────────────────┴───────────────────────┘

(
  pl.DataFrame({"a": [["a"], [], ["b", "c", "d"]]})
  .select(pl.col("a").list.pad_start(fill_value="foo"))
)

shape: (3, 1)
┌───────────────────────┐
│ a                     │
│ ---                   │
│ list[str]             │
╞═══════════════════════╡
│ ["foo", "foo", "a"]   │
│ ["foo", "foo", "foo"] │
│ ["b", "c", "d"]       │
└───────────────────────┘

Questions:

  1. In the linked issue above, the workaround lets the user specify the final length of each sublist but I use the length of the longest sublist and pad all other sublist to match this length instead. Is this correct?
  2. I feel like there's too much code duplication in the match statement, but it's not obvious to me how to reduce it. Any idea?
  3. Are the failures in new-streaming expected?

@etiennebacher etiennebacher changed the title [WIP] feat: Add list.pad_start() feat: Add list.pad_start() Jan 15, 2025
@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars and removed title needs formatting labels Jan 15, 2025
Copy link

codecov bot commented Jan 15, 2025

Codecov Report

Attention: Patch coverage is 98.03922% with 2 lines in your changes missing coverage. Please review.

Project coverage is 79.76%. Comparing base (87baf86) to head (2d38618).
Report is 30 commits behind head on main.

Files with missing lines Patch % Lines
...tes/polars-ops/src/chunked_array/list/namespace.rs 98.73% 1 Missing ⚠️
crates/polars-plan/src/dsl/function_expr/list.rs 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #20674      +/-   ##
==========================================
+ Coverage   78.85%   79.76%   +0.91%     
==========================================
  Files        1558     1561       +3     
  Lines      221055   221887     +832     
  Branches     2527     2530       +3     
==========================================
+ Hits       174318   176996    +2678     
+ Misses      46159    44309    -1850     
- Partials      578      582       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@etiennebacher etiennebacher marked this pull request as ready for review January 15, 2025 15:07
@etiennebacher
Copy link
Contributor Author

It should be possible to support more types (date, datetime, duration, maybe categorical and enum also?), but I'd like to have some feedback on the current implementation before, cf the two questions in the first post.

In particular, for now it casts categoricals to string in this case:

pl.DataFrame({"a": [["a"], ["a", "b"]]}, schema={"a": pl.List(pl.Categorical)}).select(
    pl.col("a").list.pad_start("foo")
)

# shape: (2, 1)
# ┌──────────────┐
# │ a            │
# │ ---          │
# │ list[str]    │
# ╞══════════════╡
# │ ["foo", "a"] │
# │ ["a", "b"]   │
# └──────────────┘

which I'm not sure is correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add padding method to List datatype
1 participant