fix(rust): Don't panic in rolling_skew/rolling_kurtosis on an empty Series #29179
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 437 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.364
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #29179"
general
weight: 0.35
BitWeaverDev
Summary
Every other rolling aggregation returns an empty result for an empty input:
rolling_skew/rolling_kurtosispanic instead:Because it's a Rust panic rather than a Python exception, it takes down the whole query. An empty group reaching
rolling_skew/rolling_kurtosisinsidegroup_by().agg()or.over()is enough to trigger it, not just calling it directly on an empty Series.Closes #28515.
Root cause
rolling_mean/rolling_std/rolling_var/rolling_sumall go through a shared dispatcher,rolling_aggincrates/polars-time/src/chunkedarray/rolling_window/dispatch.rs, which has an explicit guard:rolling_skew/rolling_kurtosislive in a separate, simpler file (crates/polars-ops/src/series/ops/rolling.rs) that never goes through that dispatcher and has no equivalent guard. Without it, execution reachesrolling_apply_agg_window(crates/polars-compute/src/rolling/no_nulls/mod.rs), which unconditionally initializes the aggregation window using offsets computed for index 0 viadet_offsets(0, window_size, len).det_offsetsdoesn't clamp itsendbound tolen, so forlen=0, window_size=3it returns(0, 1).MomentWindow::new(crates/polars-compute/src/rolling/moment.rs:220-225) immediately callsupdate(0, 1), which does&self.slice[self.end..new_end]=slice[0..1]on the empty input, panicking before the per-row loop (which would otherwise correctly iterate zero times) ever runs.Fix
Add the same
is_empty()early return thatrolling_aggalready uses torolling_skew_ca/rolling_kurtosis_ca, the two functions that both theSeriesmethods and the expression dispatch (crates/polars-expr/src/dispatch/rolling.rs) funnel through — one fix location covers both call surfaces the issue reports.The guard is placed after the existing numeric-to-
Float64upcast (in thedt if dt.is_primitive_numeric()branch of the publicrolling_skew/rolling_kurtosisfunctions), not before it, so that an emptyInt32input still returns an emptyFloat64result — matching what a non-emptyInt32input produces, and matchingrolling_var's existing dtype-consistent behavior on empty input.Test plan
rolling_skewandrolling_kurtosis.test_rolling_skew_kurtosis_empty_28515inpy-polars/tests/unit/operations/rolling/test_rolling.py, covering: the direct Series call, the expression/.select()call (thegroup_by/.over()code path from the issue), and the non-float-upcast dtype-consistency case.test_rolling.pyx3 files) — 318 passed, confirming non-emptyrolling_skew/rolling_kurtosisand every other rolling op are unaffected.make test— 19203 passed, 244 skipped, 7 xfailed.cargo clippy -p polars-ops --features moment --all-targets— zero warnings from the changed file.cargo fmt -p polars-ops --check— clean.