🍾 Xarray is now 10 years old! 🎉

xarray.DataArray.reset_coords

xarray.DataArray.reset_coords#

DataArray.reset_coords(names=None, *, drop=False)[source]#

Given names of coordinates, reset them to become variables.

Parameters:
  • names (str, Iterable of Hashable or None, optional) – Name(s) of non-index coordinates in this dataset to reset into variables. By default, all non-index coordinates are reset.

  • drop (bool, default: False) – If True, remove coordinates instead of converting them into variables.

Returns:

Dataset, or DataArray if ``drop == True``

Examples

>>> temperature = np.arange(25).reshape(5, 5)
>>> pressure = np.arange(50, 75).reshape(5, 5)
>>> da = xr.DataArray(
...     data=temperature,
...     dims=["x", "y"],
...     coords=dict(
...         lon=("x", np.arange(10, 15)),
...         lat=("y", np.arange(20, 25)),
...         Pressure=(["x", "y"], pressure),
...     ),
...     name="Temperature",
... )
>>> da
<xarray.DataArray 'Temperature' (x: 5, y: 5)> Size: 200B
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
Coordinates:
    lon       (x) int64 40B 10 11 12 13 14
    lat       (y) int64 40B 20 21 22 23 24
    Pressure  (x, y) int64 200B 50 51 52 53 54 55 56 57 ... 68 69 70 71 72 73 74
Dimensions without coordinates: x, y

Return Dataset with target coordinate as a data variable rather than a coordinate variable:

>>> da.reset_coords(names="Pressure")
<xarray.Dataset> Size: 480B
Dimensions:      (x: 5, y: 5)
Coordinates:
    lon          (x) int64 40B 10 11 12 13 14
    lat          (y) int64 40B 20 21 22 23 24
Dimensions without coordinates: x, y
Data variables:
    Pressure     (x, y) int64 200B 50 51 52 53 54 55 56 ... 68 69 70 71 72 73 74
    Temperature  (x, y) int64 200B 0 1 2 3 4 5 6 7 8 ... 17 18 19 20 21 22 23 24

Return DataArray without targeted coordinate:

>>> da.reset_coords(names="Pressure", drop=True)
<xarray.DataArray 'Temperature' (x: 5, y: 5)> Size: 200B
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
Coordinates:
    lon      (x) int64 40B 10 11 12 13 14
    lat      (y) int64 40B 20 21 22 23 24
Dimensions without coordinates: x, y