pola-rs/polars · error

weights not yet supported on array with null values

Error message

weights not yet supported on array with null values

What it means

nulls::rolling_var (which also backs rolling_std) computes variance over windows with null values, but its first statement panics if weights is Some. Weighted variance/std on the null-handling path is unimplemented, so nullable input plus a weights vector aborts with 'weights not yet supported on array with null values'.

Source

Thrown at crates/polars-compute/src/rolling/nulls/moment.rs:20

use num_traits::{FromPrimitive, ToPrimitive};

pub use super::super::moment::*;
use super::*;

pub fn rolling_var<T>(
    arr: &PrimitiveArray<T>,
    window_size: usize,
    min_periods: usize,
    center: bool,
    weights: Option<&[f64]>,
    params: Option<RollingFnParams>,
) -> ArrayRef
where
    T: NativeType + ToPrimitive + FromPrimitive + IsFloat + Float,
{
    if weights.is_some() {
        panic!("weights not yet supported on array with null values")
    }
    let offsets_fn = if center {
        det_offsets_center
    } else {
        det_offsets
    };
    rolling_apply_agg_window::<MomentWindow<_, VarianceMoment>, _, _, _>(
        arr.values().as_slice(),
        arr.validity().as_ref().unwrap(),
        window_size,
        min_periods,
        offsets_fn,
        params,
    )
}

pub fn rolling_skew<T>(
    arr: &PrimitiveArray<T>,

View on GitHub (pinned to 9b5d73fd00)

Solutions

  1. Remove weights and use the null-aware unweighted rolling_var/rolling_std
  2. Fill nulls before computing (document how filling biases variance) - s.fill_null(0.0).rolling_std(..., weights=...)
  3. Compute on drop_nulls() output and reindex where alignment allows
  4. Push for weighted null-aware variance support upstream

Example fix

# before
s.rolling_std(window_size=20, weights=w)  # panics when s has nulls
# after
s.fill_null(0.0).rolling_std(window_size=20, weights=w)
Defensive patterns

Strategy: validation

Validate before calling

def safe_rolling_std(s: pl.Series, window_size: int, weights=None, min_periods=1):
    if weights is not None and s.null_count() > 0:
        raise ValueError("weights + nulls unsupported for rolling_var/std")
    return s.rolling_std(window_size, weights=weights, min_periods=min_periods)

Try / catch

try:
    out = s.rolling_std(window_size=20, weights=w)
except pl.exceptions.PanicException:
    out = s.fill_null(0.0).rolling_std(window_size=20, weights=w)  # document the variance bias

Prevention

When it happens

Trigger: s.rolling_std(..., weights=...) or s.rolling_var(window_size=k, weights=[...]) on a Series containing nulls; volatility computations over price series with missing bars; .rolling_std_by with weights on nullable time index data.

Common situations: Risk/volatility pipelines: weighted rolling std over returns where some returns are null (holidays, illiquid periods); passes on synthetic complete data, panics on production gaps.

Related errors


AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19). Data as JSON: /api/errors/9353e666a71877de. Report an issue: GitHub.