xarray.DataArray.quantile

DataArray.quantile(self, q: Any, dim: Union[Hashable, Sequence[Hashable], NoneType] = None, interpolation: str = 'linear', keep_attrs: bool = None) → 'DataArray'

Compute the qth quantile of the data along the specified dimension.

Returns the qth quantiles(s) of the array elements.

Parameters
  • q (float in range of [0,1] or array-like of floats) – Quantile to compute, which must be between 0 and 1 inclusive.

  • dim (hashable or sequence of hashable, optional) – Dimension(s) over which to apply quantile.

  • interpolation ({'linear', 'lower', 'higher', 'midpoint', 'nearest'}) –

    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.

  • keep_attrs (bool, optional) – If True, the dataset’s 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 – 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 and 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

DataArray

See also

numpy.nanquantile(), pandas.Series.quantile(), Dataset.quantile()

Examples

>>> da = xr.DataArray(
...     data=[[0.7, 4.2, 9.4, 1.5], [6.5, 7.3, 2.6, 1.9]],
...     coords={"x": [7, 9], "y": [1, 1.5, 2, 2.5]},
...     dims=("x", "y"),
... )
>>> da.quantile(0)  # or da.quantile(0, dim=...)
<xarray.DataArray ()>
array(0.7)
Coordinates:
    quantile  float64 0.0
>>> da.quantile(0, dim="x")
<xarray.DataArray (y: 4)>
array([0.7, 4.2, 2.6, 1.5])
Coordinates:
  * y         (y) float64 1.0 1.5 2.0 2.5
    quantile  float64 0.0
>>> da.quantile([0, 0.5, 1])
<xarray.DataArray (quantile: 3)>
array([0.7, 3.4, 9.4])
Coordinates:
  * quantile  (quantile) float64 0.0 0.5 1.0
>>> da.quantile([0, 0.5, 1], dim="x")
<xarray.DataArray (quantile: 3, y: 4)>
array([[0.7 , 4.2 , 2.6 , 1.5 ],
       [3.6 , 5.75, 6.  , 1.7 ],
       [6.5 , 7.3 , 9.4 , 1.9 ]])
Coordinates:
  * y         (y) float64 1.0 1.5 2.0 2.5
  * quantile  (quantile) float64 0.0 0.5 1.0