🍾 Xarray is now 10 years old! 🎉

xarray.DataArray.drop_vars

xarray.DataArray.drop_vars#

DataArray.drop_vars(names, *, errors='raise')[source]#

Returns an array with dropped variables.

Parameters:
  • names (Hashable or iterable of Hashable or Callable) – Name(s) of variables to drop. If a Callable, this object is passed as its only argument and its result is used.

  • errors ({"raise", "ignore"}, default: "raise") – If ‘raise’, raises a ValueError error if any of the variable passed are not in the dataset. If ‘ignore’, any given names that are in the DataArray are dropped and no error is raised.

Returns:

dropped (Dataset) – New Dataset copied from self with variables removed.

Examples

>>> data = np.arange(12).reshape(4, 3)
>>> da = xr.DataArray(
...     data=data,
...     dims=["x", "y"],
...     coords={"x": [10, 20, 30, 40], "y": [70, 80, 90]},
... )
>>> da
<xarray.DataArray (x: 4, y: 3)> Size: 96B
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
Coordinates:
  * x        (x) int64 32B 10 20 30 40
  * y        (y) int64 24B 70 80 90

Removing a single variable:

>>> da.drop_vars("x")
<xarray.DataArray (x: 4, y: 3)> Size: 96B
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
Coordinates:
  * y        (y) int64 24B 70 80 90
Dimensions without coordinates: x

Removing a list of variables:

>>> da.drop_vars(["x", "y"])
<xarray.DataArray (x: 4, y: 3)> Size: 96B
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
Dimensions without coordinates: x, y
>>> da.drop_vars(lambda x: x.coords)
<xarray.DataArray (x: 4, y: 3)> Size: 96B
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
Dimensions without coordinates: x, y