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: Allow to use repeat_by with (nested) lists and structs #21206

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

Conversation

borchero
Copy link
Contributor

@borchero borchero commented Feb 12, 2025

Resolves #21151.

This PR introduces logic to use repeat_by with columns of arbitrarily nested lists and structs, extending the current functionality beyond primitive & binary types.

The implementation was a little hairy as the arbitrary inner dtypes of lists and structs requires plenty of dynamic type conversions. This is the first time I did something like this in polars; very happy about any feedback to make the code simpler/more efficient :)

That being said... it works flawlessly with all examples that I tried 😄


Very minimal performance test:

import polars as pl
import numpy as np


### Primitive ###
df_primitive = df = pl.DataFrame({
    "a": np.random.randint(0, 100, size=100_000),
    "b": np.random.randint(0, 5, size=100_000)
})
# %%timeit
df_primitive.select(pl.col("a").repeat_by(pl.col("b")))
# >> 2.33 ms ± 53.8 μs per loop


### List ###
df_list = pl.DataFrame({
    "a": np.random.randint(0, 100, size=1_000_000).reshape(100_000, 10),
    "b": np.random.randint(0, 5, size=100_000)
}, schema={"a": pl.List(pl.Int64), "b": pl.Int64})
# %%timeit
df_list.select(pl.col("a").repeat_by(pl.col("b")))
# >> 12.3 ms ± 185 μs per loop

@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 labels Feb 12, 2025
Copy link

codecov bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 64.82085% with 108 lines in your changes missing coverage. Please review.

Project coverage is 79.81%. Comparing base (e8e0295) to head (428db60).
Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
crates/polars-arrow/src/array/mutable_dyn/make.rs 45.33% 41 Missing ⚠️
...es/polars-arrow/src/legacy/array/nested/struct_.rs 62.85% 26 Missing ⚠️
...rates/polars-arrow/src/legacy/array/nested/list.rs 52.27% 21 Missing ⚠️
crates/polars-ops/src/chunked_array/repeat_by.rs 77.27% 10 Missing ⚠️
crates/polars-arrow/src/legacy/array/mod.rs 86.84% 5 Missing ⚠️
...rates/polars-arrow/src/io/avro/read/deserialize.rs 71.42% 2 Missing ⚠️
crates/polars-arrow/src/legacy/array/nested/mod.rs 92.85% 2 Missing ⚠️
...rates/polars-arrow/src/array/mutable_dyn/nested.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #21206      +/-   ##
==========================================
- Coverage   79.88%   79.81%   -0.08%     
==========================================
  Files        1593     1597       +4     
  Lines      227649   228071     +422     
  Branches     2600     2600              
==========================================
+ Hits       181860   182027     +167     
- Misses      45192    45447     +255     
  Partials      597      597              

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

@borchero
Copy link
Contributor Author

@alexander-beedie (since you reacted to a comment in the issue 😄) -- is there anything I can do to aid review here? I'd love to make use of this feature with the next polars release 😇

@borchero
Copy link
Contributor Author

@ritchie46 do you have any thoughts about this? Would love to drive this change along :)

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.

Allow to use repeat_by with lists
1 participant