Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Documentation
- Fix documentation of :py:class:`DataArray` removing the deprecated mention
that when omitted, `dims` are inferred from a `coords`-dict. (:pull:`3821`)
By `Sander van Rijn <https://github.com/sjvrijn>`_.
- Improve the :py:func:`where` docstring.
By `Maximilian Roos <https://github.com/max-sixty>`_

Internal Changes
~~~~~~~~~~~~~~~~
Expand Down
18 changes: 11 additions & 7 deletions xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,14 +1225,18 @@ def where(cond, x, y):
----------
cond : scalar, array, Variable, DataArray or Dataset with boolean dtype
When True, return values from `x`, otherwise returns values from `y`.
x, y : scalar, array, Variable, DataArray or Dataset
Values from which to choose. All dimension coordinates on these objects
must be aligned with each other and with `cond`.
x : scalar, array, Variable, DataArray or Dataset
values to choose from where `cond` is True
y : scalar, array, Variable, DataArray or Dataset
values to choose from where `cond` is False

All dimension coordinates on these objects must be aligned with each
other and with `cond`.

Returns
-------
In priority order: Dataset, DataArray, Variable or array, whichever
type appears as an input argument.
Dataset, DataArray, Variable or array, whichever type appears as an input
argument.

Examples
--------
Expand All @@ -1246,13 +1250,13 @@ def where(cond, x, y):
Coordinates:
* lat (lat) int64 0 1 2 3 4 5 6 7 8 9

>>> xr.where(x < 0.5, x, 100*x)
>>> xr.where(x < 0.5, x, x * 100)
<xarray.DataArray 'sst' (lat: 10)>
array([ 0. , 0.1, 0.2, 0.3, 0.4, 50. , 60. , 70. , 80. , 90. ])
Coordinates:
* lat (lat) int64 0 1 2 3 4 5 6 7 8 9

>>> >>> y = xr.DataArray(
>>> y = xr.DataArray(
... 0.1 * np.arange(9).reshape(3, 3),
... dims=["lat", "lon"],
... coords={"lat": np.arange(3), "lon": 10 + np.arange(3)},
Expand Down
4 changes: 1 addition & 3 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4362,8 +4362,6 @@ def assign(

Examples
--------
>>> import numpy as np
>>> import xarray as xr
>>> x = xr.Dataset(
... {
... "temperature_c": (("lat", "lon"), 20 * np.random.rand(4).reshape(2, 2)),
Expand All @@ -4383,7 +4381,7 @@ def assign(

Where the value is a callable, evaluated on dataset:

>>> x.assign(temperature_f = lambda x: x.temperature_c * 9 / 5 + 32)
>>> x.assign(temperature_f=lambda x: x.temperature_c * 9 / 5 + 32)
<xarray.Dataset>
Dimensions: (lat: 2, lon: 2)
Coordinates:
Expand Down