{"record":{"id":"6016ee0e6b9248d5","repo":"pola-rs/polars","slug":"weights-not-yet-supported-on-array-with-null-value","errorCode":null,"errorMessage":"weights not yet supported on array with null values","messagePattern":"weights not yet supported on array with null values","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-compute/src/rolling/nulls/mean.rs","lineNumber":25,"sourceCode":"    window_size: usize,\n    min_periods: usize,\n    center: bool,\n    weights: Option<&[f64]>,\n    _params: Option<RollingFnParams>,\n) -> ArrayRef\nwhere\n    T: NativeType\n        + IsFloat\n        + PartialOrd\n        + Add<Output = T>\n        + Sub<Output = T>\n        + NumCast\n        + AddAssign\n        + SubAssign\n        + Div<Output = T>,\n{\n    if weights.is_some() {\n        panic!(\"weights not yet supported on array with null values\")\n    }\n    if center {\n        rolling_apply_agg_window::<MeanWindow<T>, _, _, _>(\n            arr.values().as_slice(),\n            arr.validity().as_ref().unwrap(),\n            window_size,\n            min_periods,\n            det_offsets_center,\n            None,\n        )\n    } else {\n        rolling_apply_agg_window::<MeanWindow<T>, _, _, _>(\n            arr.values().as_slice(),\n            arr.validity().as_ref().unwrap(),\n            window_size,\n            min_periods,\n            det_offsets,\n            None,","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/pola-rs/polars/blob/9b5d73fd00236295624374b075d16b1fe6ec6df9/crates/polars-compute/src/rolling/nulls/mean.rs#L7-L43","documentation":"The null-handling rolling kernels in polars-compute (nulls::rolling_mean and siblings) implement only unweighted aggregation. rolling_mean starts with an explicit panic guard: if weights is Some while the input array contains nulls, it panics 'weights not yet supported on array with null values' rather than returning silently wrong numbers. Weighted rolling works only on the no-nulls code path.","triggerScenarios":"s.rolling_mean(window_size=3, weights=[0.2, 0.3, 0.5]) - or .rolling_mean(..., weights=...) in an expression - on a Series or group that contains at least one null value; polars dispatches to the nulls path (null_count > 0) and the weight guard fires.","commonSituations":"Time-series with missing observations: code developed on gap-free test data passes weights fine, production data with nulls panics; group_by().agg(rolling_mean with weights) where some groups contain nulls; lazy queries where nulls appear only after a join.","solutions":["Fill or drop nulls before the rolling call: s.fill_null(0.0).rolling_mean(...) (note filling changes the statistic) or s.drop_nulls() (shifts window alignment)","Omit the weights argument - the unweighted null-handling path is fully implemented","Compute a weighted mean from unweighted ops after filling: rolling_sum(x*w) / rolling_sum(w)","If exact weighted-with-nulls semantics are required, implement it upstream (weight window aggregation by validity) or file a feature request"],"exampleFix":"# before\ns.rolling_mean(window_size=3, weights=[0.2, 0.3, 0.5])  # panics when s has nulls\n# after\ns_filled = s.fill_null(0.0)\ns_filled.rolling_mean(window_size=3, weights=[0.2, 0.3, 0.5])","handlingStrategy":"validation","validationCode":"def safe_rolling_mean(s: pl.Series, window_size: int, weights=None, min_periods=1):\n    if weights is not None and s.null_count() > 0:\n        raise ValueError(\n            \"weights + null values unsupported: fill or drop nulls, or drop weights\"\n        )\n    return s.rolling_mean(window_size, weights=weights, min_periods=min_periods)","typeGuard":null,"tryCatchPattern":"try:\n    out = s.rolling_mean(window_size=3, weights=w)\nexcept pl.exceptions.PanicException:\n    out = s.fill_null(0.0).rolling_mean(window_size=3, weights=w)  # document the bias","preventionTips":["Check null_count() before passing weights to any rolling_* function","Fill or drop nulls at ingest for series destined for weighted rolling stats","Assert data quality (no unexpected nulls) before weighted computations in production"],"tags":["polars","rolling","rolling-mean","weights","null-values","panic"],"backgroundTag":"rolling-weights-with-nulls","analyzedSha":"9b5d73fd00236295624374b075d16b1fe6ec6df9","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}