Skip to content

DOC: add query examples #5249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4452,6 +4452,17 @@ def query(
Dataset.query
pandas.eval

Examples
--------
>>> da = xr.DataArray(np.arange(0, 5, 1), dims="x", name="a")
>>> da
<xarray.DataArray 'a' (x: 5)>
array([0, 1, 2, 3, 4])
Dimensions without coordinates: x
>>> da.query(x="a > 2")
<xarray.DataArray 'a' (x: 2)>
array([3, 4])
Dimensions without coordinates: x
"""

ds = self._to_dataset_whole(shallow_copy=True)
Expand Down
19 changes: 19 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7235,6 +7235,25 @@ def query(
Dataset.isel
pandas.eval

Examples
--------
>>> a = np.arange(0, 5, 1)
>>> b = np.linspace(0, 1, 5)
>>> ds = xr.Dataset({"a": ("x", a), "b": ("x", b)})
>>> ds
<xarray.Dataset>
Dimensions: (x: 5)
Dimensions without coordinates: x
Data variables:
a (x) int64 0 1 2 3 4
b (x) float64 0.0 0.25 0.5 0.75 1.0
>>> ds.query(x="a > 2")
<xarray.Dataset>
Dimensions: (x: 2)
Dimensions without coordinates: x
Data variables:
a (x) int64 3 4
b (x) float64 0.75 1.0
"""

# allow queries to be given either as a dict or as kwargs
Expand Down