xarray.core.weighted.DataArrayWeighted.quantile#
- DataArrayWeighted.quantile(q, *, dim=None, keep_attrs=None, skipna=True)[source]#
Apply a weighted
quantile
to this Dataset’s data along some dimension(s).Weights are interpreted as sampling weights (or probability weights) and describe how a sample is scaled to the whole population 1. There are other possible interpretations for weights, precision weights describing the precision of observations, or frequency weights counting the number of identical observations, however, they are not implemented here.
For compatibility with NumPy’s non-weighted
quantile
(which is used byDataArray.quantile
andDataset.quantile
), the only interpolation method supported by this weighted version corresponds to the default “linear” option ofnumpy.quantile
. This is “Type 7” option, described in Hyndman and Fan (1996) 2. The implementation is largely inspired by a blog post from A. Akinshin’s (2023) 3.- Parameters
q (
float
or sequence offloat
) – Quantile to compute, which must be between 0 and 1 inclusive.dim (
str
or sequence ofstr
, optional) – Dimension(s) over which to apply the weightedquantile
.skipna (
bool
, optional) – If True, skip missing values (as marked by NaN). By default, only skips missing values for float dtypes; other dtypes either do not have a sentinel missing value (int) or skipna=True has not been implemented (object, datetime64 or timedelta64).keep_attrs (
bool
, optional) – If True, the attributes (attrs
) will be copied from the original object to the new one. If False (default), the new object will be returned without attributes.
- Returns
quantiles (
Dataset
) – New Dataset object with weightedquantile
applied to its data and the indicated dimension(s) removed.
See also
numpy.nanquantile
,pandas.Series.quantile
,Dataset.quantile
,DataArray.quantile
Notes
Returns NaN if the
weights
sum to 0.0 along the reduced dimension(s).References
- 1
https://notstatschat.rbind.io/2020/08/04/weights-in-statistics/
- 2
Hyndman, R. J. & Fan, Y. (1996). Sample Quantiles in Statistical Packages. The American Statistician, 50(4), 361–365. https://doi.org/10.2307/2684934
- 3
Akinshin, A. (2023) “Weighted quantile estimators” arXiv:2304.07265 [stat.ME] https://arxiv.org/abs/2304.07265