← Back to Pull requests

fix(rust): Don't panic in rolling_skew/rolling_kurtosis on an empty Series #29179

Closed
unknown closed this pull request without merging on Sep 07, 2026 10:42 UTC
View on GitHub
unknown unknown commented on Sep 06, 2026 10:10 UTC
Author

Summary

Every other rolling aggregation returns an empty result for an empty input:

pl.Series("x", [], dtype=pl.Float64).rolling_std(window_size=3)   # shape: (0,)
pl.Series("x", [], dtype=pl.Float64).rolling_skew(window_size=3)  # panics

rolling_skew/rolling_kurtosis panic instead:

thread '<unnamed>' panicked at crates/polars-compute/src/rolling/moment.rs:243:31:
range end index 1 out of range for slice of length 0

Because it's a Rust panic rather than a Python exception, it takes down the whole query. An empty group reaching rolling_skew/rolling_kurtosis inside group_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_sum all go through a shared dispatcher, rolling_agg in crates/polars-time/src/chunkedarray/rolling_window/dispatch.rs, which has an explicit guard:

if ca.is_empty() {
    return Ok(Series::new_empty(ca.name().clone(), ca.dtype()));
}

rolling_skew/rolling_kurtosis live 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 reaches rolling_apply_agg_window (crates/polars-compute/src/rolling/no_nulls/mod.rs), which unconditionally initializes the aggregation window using offsets computed for index 0 via det_offsets(0, window_size, len). det_offsets doesn't clamp its end bound to len, so for len=0, window_size=3 it returns (0, 1). MomentWindow::new (crates/polars-compute/src/rolling/moment.rs:220-225) immediately calls update(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 that rolling_agg already uses to rolling_skew_ca/rolling_kurtosis_ca, the two functions that both the Series methods 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-Float64 upcast (in the dt if dt.is_primitive_numeric() branch of the public rolling_skew/rolling_kurtosis functions), not before it, so that an empty Int32 input still returns an empty Float64 result — matching what a non-empty Int32 input produces, and matching rolling_var's existing dtype-consistent behavior on empty input.

Test plan

  • Reproduced the exact panic from the issue against an unpatched build; confirmed it's gone after the fix, for both rolling_skew and rolling_kurtosis.

  • Added test_rolling_skew_kurtosis_empty_28515 in py-polars/tests/unit/operations/rolling/test_rolling.py, covering: the direct Series call, the expression/.select() call (the group_by/.over() code path from the issue), and the non-float-upcast dtype-consistency case.

  • Ran the full rolling test suite (test_rolling.py x3 files) — 318 passed, confirming non-empty rolling_skew/rolling_kurtosis and 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.

  • ruff, ruff format, mypy on the test file — clean.
  • Diff: 437 lines (+327 / -110) across 5 files Reviewer: BitWeaverDev Review duration: 1472m
    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
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.2% (MEDIUM) · Higher = larger blast radius
    45.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.9% · Safe to merge
    17.9%
    Repository