Skip to content

Conversation

coastalwhite
Copy link
Collaborator

@coastalwhite coastalwhite commented Sep 12, 2025

This implements array.eval which allows running arbitrary expressions on array elements. This is the counterpart of list.eval but for the Array datatype.

>>> import polars as pl
>>> s = pl.Series("a", [[1, 4], [8, 5], [3, 2]], pl.Array(pl.Int64, 2))
>>> s.arr.eval(pl.element().rank())
shape: (3,)
Series: 'a' [array[f64, 2]]
[
	[1.0, 2.0]
	[2.0, 1.0]
	[2.0, 1.0]
]

The arr.eval method by default only allows-for length-preserving expressions to be run and will map from an Array datatype with a certain width to another Array with the same width.

If you want to run any non-length-preserving expression, you can set as_list=True which will cause the expression to output a List.

>>> s = pl.Series(
...     "a", [["A", "B", "C"], ["C", "C", "D"], ["D", "E", "E"]], pl.Array(pl.String, 3)
... )
>>> s.arr.eval(pl.element().unique(), as_list=True)
shape: (3,)
Series: 'a' [list[str]]
[
	["C", "B", "A"]
	["C", "D"]
	["D", "E"]
]

xref: #21302.

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

codecov bot commented Sep 12, 2025

Codecov Report

❌ Patch coverage is 89.09774% with 29 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.79%. Comparing base (e54330b) to head (10b7d54).
⚠️ Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
crates/polars-arrow/src/array/list/mod.rs 0.00% 10 Missing ⚠️
crates/polars-core/src/chunked_array/mod.rs 93.18% 6 Missing ⚠️
crates/polars-expr/src/expressions/eval.rs 94.73% 5 Missing ⚠️
crates/polars-plan/src/dsl/expr/mod.rs 83.33% 3 Missing ⚠️
crates/polars-core/src/frame/group_by/position.rs 71.42% 2 Missing ⚠️
crates/polars-plan/src/dsl/format.rs 0.00% 2 Missing ⚠️
crates/polars-plan/src/plans/aexpr/mod.rs 94.73% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main   #24472    +/-   ##
========================================
  Coverage   81.78%   81.79%            
========================================
  Files        1681     1684     +3     
  Lines      228078   228785   +707     
  Branches     2923     2929     +6     
========================================
+ Hits       186533   187129   +596     
- Misses      40812    40919   +107     
- Partials      733      737     +4     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

.collect()
} else {
(0..ca.len())
.map(|i| [(i * ca.width()) as IdxSize, ca.width() as IdxSize])
Copy link
Member

Choose a reason for hiding this comment

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

Where do we check if len * width < IdxSize::MAX?

@ritchie46 ritchie46 force-pushed the main branch 3 times, most recently from ddf5907 to d0914d4 Compare September 27, 2025 11:06
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 merge-after-dsl-unfreeze 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