nanaverage

nanaverage(a, weights=None, axis=None, *, ignore_inf=False, validate=True)

Return the weighted average while skipping NaN values.

Parameters

a : array_like

Input data. Supported numeric inputs are normalized to a contiguous kernel array when validate=True. Computed reducers promote integer and bool inputs to float64; exact selection reducers keep integer and bool dtypes where the selected value can be returned exactly. Complex and object arrays are not supported.

axis : None, 0, -1, or int = None

Axis to reduce. None reduces the whole array. 0 reduces strided reducing-axis slices into the remaining shape. -1 and ndim - 1 reduce contiguous slices. Other axes raise NotImplementedError.

weights : None or array_like = None

Weights for a weighted reduction. With axis=None, weights must have the same shape as a. With an axis reduction, weights may either have the same shape as a or be 1-D with length equal to the reducing axis.

ignore_inf : bool = False

If False, skip only NaN values and keep +/-inf values, matching NumPy’s nan* reducers. If True, skip all non-finite values.

validate : bool = True

If True, check dtype, dimensionality, contiguity, and axis validity before entering the Rust kernel. If False, the caller must provide a contiguous supported kernel dtype (float32, float64, bool, or a NumPy integer dtype). validate=False skips dtype promotion: integer and bool arrays are reduced directly, while complex and object arrays remain unsupported.

Returns

out : float or ndarray

Reduction result. axis=None returns a Python float. Axis reductions return an array with the reduced axis removed.

Notes

NaN-aware reducers skip NaN values directly, without building a filtered copy of the input.

Weighted averages return floating results, so integer and bool inputs are promoted. A zero sum of retained weights raises ZeroDivisionError. For nanaverage, NaN values in a are skipped (with ignore_inf=True, all non-finite values in a are skipped), and the weights attached to skipped values are skipped with them; a NaN weight on a retained value propagates to the result.