🍾 Xarray is now 10 years old! πŸŽ‰

xarray.Coordinates.assign

xarray.Coordinates.assign#

Coordinates.assign(coords=None, **coords_kwargs)[source]#

Assign new coordinates (and indexes) to a Coordinates object, returning a new object with all the original coordinates in addition to the new ones.

Parameters:
  • coords (mapping of dim to coord, optional) – A mapping whose keys are the names of the coordinates and values are the coordinates to assign. The mapping will generally be a dict or Coordinates.

    • If a value is a standard data value β€” for example, a DataArray, scalar, or array β€” the data is simply assigned as a coordinate.

    • A coordinate can also be defined and attached to an existing dimension using a tuple with the first element the dimension name and the second element the values for this new coordinate.

  • **coords_kwargs – The keyword arguments form of coords. One of coords or coords_kwargs must be provided.

Returns:

new_coords (Coordinates) – A new Coordinates object with the new coordinates (and indexes) in addition to all the existing coordinates.

Examples

>>> coords = xr.Coordinates()
>>> coords
Coordinates:
    *empty*
>>> coords.assign(x=[1, 2])
Coordinates:
  * x        (x) int64 16B 1 2
>>> midx = pd.MultiIndex.from_product([["a", "b"], [0, 1]])
>>> coords.assign(xr.Coordinates.from_pandas_multiindex(midx, "y"))
Coordinates:
  * y          (y) object 32B MultiIndex
  * y_level_0  (y) object 32B 'a' 'a' 'b' 'b'
  * y_level_1  (y) int64 32B 0 1 0 1