Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
Examples for quantile (pydata#3576)
Browse files Browse the repository at this point in the history
* add examples for Dataset.quantile

* add examples for DataArray.quantile

* add examples for GroupBy.quantile

* rename the example sections
  • Loading branch information
keewis authored and max-sixty committed Nov 28, 2019
1 parent 4c26d6a commit be2ff9d
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
37 changes: 37 additions & 0 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2971,6 +2971,43 @@ def quantile(
See Also
--------
numpy.nanpercentile, 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"),
... )
Single quantile
>>> 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
Multiple quantiles
>>> 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
"""

ds = self._to_temp_dataset().quantile(
Expand Down
42 changes: 42 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5116,6 +5116,48 @@ def quantile(
See Also
--------
numpy.nanpercentile, pandas.Series.quantile, DataArray.quantile
Examples
--------
>>> ds = xr.Dataset(
... {"a": (("x", "y"), [[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]},
... )
Single quantile
>>> ds.quantile(0) # or ds.quantile(0, dim=...)
<xarray.Dataset>
Dimensions: ()
Coordinates:
quantile float64 0.0
Data variables:
a float64 0.7
>>> ds.quantile(0, dim="x")
<xarray.Dataset>
Dimensions: (y: 4)
Coordinates:
* y (y) float64 1.0 1.5 2.0 2.5
quantile float64 0.0
Data variables:
a (y) float64 0.7 4.2 2.6 1.5
Multiple quantiles
>>> ds.quantile([0, 0.5, 1])
<xarray.Dataset>
Dimensions: (quantile: 3)
Coordinates:
* quantile (quantile) float64 0.0 0.5 1.0
Data variables:
a (quantile) float64 0.7 3.4 9.4
>>> ds.quantile([0, 0.5, 1], dim="x")
<xarray.Dataset>
Dimensions: (quantile: 3, y: 4)
Coordinates:
* y (y) float64 1.0 1.5 2.0 2.5
* quantile (quantile) float64 0.0 0.5 1.0
Data variables:
a (quantile, y) float64 0.7 4.2 2.6 1.5 3.6 ... 1.7 6.5 7.3 9.4 1.9
"""

if isinstance(dim, str):
Expand Down
53 changes: 53 additions & 0 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,59 @@ def quantile(self, q, dim=None, interpolation="linear", keep_attrs=None):
--------
numpy.nanpercentile, 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=("y", "y"),
... )
>>> ds = xr.Dataset({"a": da})
Single quantile
>>> 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:
quantile float64 0.0
* y (y) int64 1 1 2 2
* 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
Multiple quantiles
>>> 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
"""
if dim is None:
dim = self._group_dim
Expand Down

0 comments on commit be2ff9d

Please sign in to comment.