Skip to content

feat: Add cum_mean Expression and Series method#26569

Open
alexander-beedie wants to merge 5 commits intopola-rs:mainfrom
alexander-beedie:feat-cum-mean
Open

feat: Add cum_mean Expression and Series method#26569
alexander-beedie wants to merge 5 commits intopola-rs:mainfrom
alexander-beedie:feat-cum-mean

Conversation

@alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented Feb 14, 2026

Also closes #26065.

Been working on this one on & off for a few weeks; ready for review 🤔

Since we need to track state for two variables, added a CumAggState enum...

pub enum CumAggState {
    Single(AnyValue<'static>),
    Mean(CumMeanState),
}

...with all the existing cumulative exprs using the single-value option, and mean using the new CumMeanState, which tracks the sum and count vars (for float and -if indicated- decimal). Can extend in the future if any other aggregate functions need additional state tracking.

The cumulative mean calculation itself is relatively unexciting (though there is a dedicated Decimal path, patterned after the same in the other cumulative funcs). It got much better this morning when I found the existing KahanSum struct and was able to (almost) just drop it right in, making the overall implementation feel much improved 👍

We had (relatively) little dedicated cumulative expression coverage, so I added more in a new test_cumulative.py file, covering additional validations such as streaming vs in-memory, optimised/unoptimised, nan poisoning, inf behaviour, and the full range of int/float/temporal dtypes (slightly over half of this PR is just new test coverage).


🤖 AI: Claude Code (Opus 4.6) helped identify test coverage gaps and provided the initial unit test skeleton, which I then industrialised and made more idiomatic.

@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 14, 2026
@alexander-beedie alexander-beedie changed the title feat: Add cum_mean Expression feat: Add cum_mean Expression and Series method Feb 14, 2026
@github-actions github-actions bot added the changes-dsl Do not merge if this label is present and red. label Feb 14, 2026
@codecov
Copy link

codecov bot commented Feb 14, 2026

Codecov Report

❌ Patch coverage is 89.41799% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.39%. Comparing base (2892ced) to head (f8abe95).

Files with missing lines Patch % Lines
crates/polars-ops/src/series/ops/cum_agg.rs 90.09% 11 Missing ⚠️
crates/polars-stream/src/nodes/cum_agg.rs 89.74% 4 Missing ⚠️
crates/polars-plan/src/dsl/function_expr/mod.rs 0.00% 2 Missing ⚠️
...s/polars-plan/src/plans/aexpr/function_expr/cum.rs 90.00% 1 Missing ⚠️
...s/polars-plan/src/plans/aexpr/function_expr/mod.rs 66.66% 1 Missing ⚠️
.../polars-python/src/lazyframe/visitor/expr_nodes.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #26569      +/-   ##
==========================================
+ Coverage   81.38%   81.39%   +0.01%     
==========================================
  Files        1792     1792              
  Lines      244414   244583     +169     
  Branches     3078     3078              
==========================================
+ Hits       198913   199084     +171     
+ Misses      44715    44713       -2     
  Partials      786      786              

☔ 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changes-dsl Do not merge if this label is present and red. 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.

bug: AVG OVER WINDOW returns incorrect values

2 participants

Comments