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.

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>
Dimensions:        (lat: 2, lon: 2)
Coordinates:
  * lat            (lat) int64 10 20
  * lon            (lon) int64 150 160
Data variables:
    temperature_c  (lat, lon) float64 10.98 14.3 12.06 10.9
    precipitation  (lat, lon) float64 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>
Dimensions:        (lat: 2, lon: 2)
Coordinates:
  * lat            (lat) int64 10 20
  * lon            (lon) int64 150 160
Data variables:
    temperature_c  (lat, lon) float64 10.98 14.3 12.06 10.9
    precipitation  (lat, lon) float64 0.4237 0.6459 0.4376 0.8918
    temperature_f  (lat, lon) float64 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>
Dimensions:        (lat: 2, lon: 2)
Coordinates:
  * lat            (lat) int64 10 20
  * lon            (lon) int64 150 160
Data variables:
    temperature_c  (lat, lon) float64 10.98 14.3 12.06 10.9
    precipitation  (lat, lon) float64 0.4237 0.6459 0.4376 0.8918
    temperature_f  (lat, lon) float64 51.76 57.75 53.7 51.62