How do I …

How do I …#

How do I…

Solution

add a DataArray to my dataset as a new variable

my_dataset[varname] = my_dataArray or Dataset.assign() (see also Dictionary like methods)

add variables from other datasets to my dataset

Dataset.merge()

add a new dimension and/or coordinate

DataArray.expand_dims(), Dataset.expand_dims()

add a new coordinate variable

DataArray.assign_coords()

change a data variable to a coordinate variable

Dataset.set_coords()

change the order of dimensions

DataArray.transpose(), Dataset.transpose()

reshape dimensions

DataArray.stack(), Dataset.stack(), Dataset.coarsen.construct(), DataArray.coarsen.construct()

remove a variable from my object

Dataset.drop_vars(), DataArray.drop_vars()

remove dimensions of length 1 or 0

DataArray.squeeze(), Dataset.squeeze()

remove all variables with a particular dimension

Dataset.drop_dims()

convert non-dimension coordinates to data variables or remove them

DataArray.reset_coords(), Dataset.reset_coords()

rename a variable, dimension or coordinate

Dataset.rename(), DataArray.rename(), Dataset.rename_vars(), Dataset.rename_dims(),

convert a DataArray to Dataset or vice versa

DataArray.to_dataset(), Dataset.to_dataarray(), Dataset.to_stacked_array(), DataArray.to_unstacked_dataset()

extract variables that have certain attributes

Dataset.filter_by_attrs()

extract the underlying array (e.g. NumPy or Dask arrays)

DataArray.data

convert to and extract the underlying NumPy array

DataArray.to_numpy

convert to a pandas DataFrame

Dataset.to_dataframe

sort values

Dataset.sortby

find out if my xarray object is wrapping a Dask Array

dask.is_dask_collection()

know how much memory my object requires

DataArray.nbytes, Dataset.nbytes

Get axis number for a dimension

DataArray.get_axis_num()

convert a possibly irregularly sampled timeseries to a regularly sampled timeseries

DataArray.resample(), Dataset.resample() (see Resampling and grouped operations for more)

apply a function on all data variables in a Dataset

Dataset.map()

write xarray objects with complex values to a netCDF file

Dataset.to_netcdf(), DataArray.to_netcdf() specifying engine="h5netcdf" or Dataset.to_netcdf(), DataArray.to_netcdf() specifying engine="netCDF4", auto_complex=True

make xarray objects look like other xarray objects

ones_like(), zeros_like(), full_like(), Dataset.reindex_like(), Dataset.interp_like(), Dataset.broadcast_like(), DataArray.reindex_like(), DataArray.interp_like(), DataArray.broadcast_like()

Make sure my datasets have values at the same coordinate locations

xr.align(dataset_1, dataset_2, join="exact")

replace NaNs with other values

Dataset.fillna(), Dataset.ffill(), Dataset.bfill(), Dataset.interpolate_na(), DataArray.fillna(), DataArray.ffill(), DataArray.bfill(), DataArray.interpolate_na()

extract the year, month, day or similar from a DataArray of time values

obj.dt.month for example where obj is a DataArray containing datetime64 or cftime values. See Datetime components for more.

round off time values to a specified frequency

obj.dt.ceil, obj.dt.floor, obj.dt.round. See Datetime components for more.

make a mask that is True where an object contains any of the values in a array

Dataset.isin(), DataArray.isin()

Index using a boolean mask

Dataset.query(), DataArray.query(), Dataset.where(), DataArray.where()

preserve attrs during (most) xarray operations

xr.set_options(keep_attrs=True)