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 (Coordinates or mapping of hashable to Any) โ€“ Mapping from coordinate names to the new values. If a Coordinates object is passed, its indexes are assigned in the returned object. Otherwise, a default (pandas) index is created for each dimension coordinate found in the mapping.

  • **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 1 2
>>> midx = pd.MultiIndex.from_product([["a", "b"], [0, 1]])
>>> coords.assign(xr.Coordinates.from_pandas_multiindex(midx, "y"))
Coordinates:
  * y          (y) object MultiIndex
  * y_level_0  (y) object 'a' 'a' 'b' 'b'
  * y_level_1  (y) int64 0 1 0 1