๐Ÿพ Xarray is now 10 years old! ๐ŸŽ‰

xarray.Dataset.assign

Contents

xarray.Dataset.assign#

Dataset.assign(variables=None, **variables_kwargs)[source]#

Assign new data variables to a Dataset, returning a new object with all the original variables in addition to the new ones.

Parameters:
  • variables (mapping of hashable to Any) โ€“ Mapping from variables names to the new values. If the new values are callable, they are computed on the Dataset and assigned to new data variables. If the values are not callable, (e.g. a DataArray, scalar, or array), they are simply assigned.

  • **variables_kwargs โ€“ The keyword arguments form of variables. One of variables or variables_kwargs must be provided.

Returns:

ds (Dataset) โ€“ A new Dataset with the new variables in addition to all the existing variables.

Notes

Since kwargs is a dictionary, the order of your arguments may not be preserved, and so the order of the new variables is not well defined. Assigning multiple variables within the same assign is possible, but you cannot reference other variables created within the same assign call.

The new assigned variables that replace existing coordinates in the original dataset are still listed as coordinates in the returned Dataset.

Examples

>>> x = xr.Dataset(
...     {
...         "temperature_c": (
...             ("lat", "lon"),
...             20 * np.random.rand(4).reshape(2, 2),
...         ),
...         "precipitation": (("lat", "lon"), np.random.rand(4).reshape(2, 2)),
...     },
...     coords={"lat": [10, 20], "lon": [150, 160]},
... )
>>> x
<xarray.Dataset> Size: 96B
Dimensions:        (lat: 2, lon: 2)
Coordinates:
  * lat            (lat) int64 16B 10 20
  * lon            (lon) int64 16B 150 160
Data variables:
    temperature_c  (lat, lon) float64 32B 10.98 14.3 12.06 10.9
    precipitation  (lat, lon) float64 32B 0.4237 0.6459 0.4376 0.8918

Where the value is a callable, evaluated on dataset:

>>> x.assign(temperature_f=lambda x: x.temperature_c * 9 / 5 + 32)
<xarray.Dataset> Size: 128B
Dimensions:        (lat: 2, lon: 2)
Coordinates:
  * lat            (lat) int64 16B 10 20
  * lon            (lon) int64 16B 150 160
Data variables:
    temperature_c  (lat, lon) float64 32B 10.98 14.3 12.06 10.9
    precipitation  (lat, lon) float64 32B 0.4237 0.6459 0.4376 0.8918
    temperature_f  (lat, lon) float64 32B 51.76 57.75 53.7 51.62

Alternatively, the same behavior can be achieved by directly referencing an existing dataarray:

>>> x.assign(temperature_f=x["temperature_c"] * 9 / 5 + 32)
<xarray.Dataset> Size: 128B
Dimensions:        (lat: 2, lon: 2)
Coordinates:
  * lat            (lat) int64 16B 10 20
  * lon            (lon) int64 16B 150 160
Data variables:
    temperature_c  (lat, lon) float64 32B 10.98 14.3 12.06 10.9
    precipitation  (lat, lon) float64 32B 0.4237 0.6459 0.4376 0.8918
    temperature_f  (lat, lon) float64 32B 51.76 57.75 53.7 51.62