xarray.core.groupby.DataArrayGroupBy.quantile

DataArrayGroupBy.quantile(q, dim=None, interpolation='linear', keep_attrs=None, skipna=True)

Compute the qth quantile over each array in the groups and concatenate them together into a new array.

Parameters
  • q (float or sequence of float) – Quantile to compute, which must be between 0 and 1 inclusive.

  • dim (..., str or sequence of str, optional) – Dimension(s) over which to apply quantile. Defaults to the grouped dimension.

  • interpolation ({"linear", "lower", "higher", "midpoint", "nearest"}, default: "linear") – This optional parameter specifies the interpolation method to use when the desired quantile lies between two data points i < j:

    • linear: i + (j - i) * fraction, where fraction is the fractional part of the index surrounded by i and j.

    • lower: i.

    • higher: j.

    • nearest: i or j, whichever is nearest.

    • midpoint: (i + j) / 2.

  • skipna (bool, optional) – Whether to skip missing values when aggregating.

Returns

quantiles – If q is a single quantile, then the result is a scalar. If multiple percentiles are given, first axis of the result corresponds to the quantile. In either case a quantile dimension is added to the return array. The other dimensions are the dimensions that remain after the reduction of the array.

Return type

Variable

See also

numpy.nanquantile, numpy.quantile, pandas.Series.quantile, Dataset.quantile, DataArray.quantile

Examples

>>> da = xr.DataArray(
...     [[1.3, 8.4, 0.7, 6.9], [0.7, 4.2, 9.4, 1.5], [6.5, 7.3, 2.6, 1.9]],
...     coords={"x": [0, 0, 1], "y": [1, 1, 2, 2]},
...     dims=("x", "y"),
... )
>>> ds = xr.Dataset({"a": da})
>>> da.groupby("x").quantile(0)
<xarray.DataArray (x: 2, y: 4)>
array([[0.7, 4.2, 0.7, 1.5],
       [6.5, 7.3, 2.6, 1.9]])
Coordinates:
  * y         (y) int64 1 1 2 2
    quantile  float64 0.0
  * x         (x) int64 0 1
>>> ds.groupby("y").quantile(0, dim=...)
<xarray.Dataset>
Dimensions:   (y: 2)
Coordinates:
    quantile  float64 0.0
  * y         (y) int64 1 2
Data variables:
    a         (y) float64 0.7 0.7
>>> da.groupby("x").quantile([0, 0.5, 1])
<xarray.DataArray (x: 2, y: 4, quantile: 3)>
array([[[0.7 , 1.  , 1.3 ],
        [4.2 , 6.3 , 8.4 ],
        [0.7 , 5.05, 9.4 ],
        [1.5 , 4.2 , 6.9 ]],

       [[6.5 , 6.5 , 6.5 ],
        [7.3 , 7.3 , 7.3 ],
        [2.6 , 2.6 , 2.6 ],
        [1.9 , 1.9 , 1.9 ]]])
Coordinates:
  * y         (y) int64 1 1 2 2
  * quantile  (quantile) float64 0.0 0.5 1.0
  * x         (x) int64 0 1
>>> ds.groupby("y").quantile([0, 0.5, 1], dim=...)
<xarray.Dataset>
Dimensions:   (quantile: 3, y: 2)
Coordinates:
  * quantile  (quantile) float64 0.0 0.5 1.0
  * y         (y) int64 1 2
Data variables:
    a         (y, quantile) float64 0.7 5.35 8.4 0.7 2.25 9.4