xarray.Dataset.expand_dims#
- Dataset.expand_dims(dim=None, axis=None, create_index_for_new_dim=True, **dim_kwargs)[source]#
Return a new object with an additional axis (or axes) inserted at the corresponding position in the array shape. The new object is a view into the underlying array, not a copy.
If dim is already a scalar coordinate, it will be promoted to a 1D coordinate consisting of a single value.
The automatic creation of indexes to back new 1D coordinate variables controlled by the create_index_for_new_dim kwarg.
- Parameters
dim (hashable, sequence of hashable, mapping, or
None
) – Dimensions to include on the new variable. If provided as hashable or sequence of hashable, then dimensions are inserted with length 1. If provided as a mapping, then the keys are the new dimensions and the values are either integers (giving the length of the new dimensions) or array-like (giving the coordinates of the new dimensions).axis (
int
, sequence ofint
, orNone
, default:None
) – Axis position(s) where new axis is to be inserted (position(s) on the result array). If a sequence of integers is passed, multiple axes are inserted. In this case, dim arguments should be same length list. If axis=None is passed, all the axes will be inserted to the start of the result array.create_index_for_new_dim (
bool
, default:True
) – Whether to create newPandasIndex
objects when the object being expanded contains scalar variables with names indim
.**dim_kwargs (
int
or sequence orndarray
) – The keywords are arbitrary dimensions being inserted and the values are either the lengths of the new dims (if int is given), or their coordinates. Note, this is an alternative to passing a dict to the dim kwarg and will only be used if dim is None.
- Returns
expanded (
Dataset
) – This object, but with additional dimension(s).
Examples
>>> dataset = xr.Dataset({"temperature": ([], 25.0)}) >>> dataset <xarray.Dataset> Size: 8B Dimensions: () Data variables: temperature float64 8B 25.0
# Expand the dataset with a new dimension called “time”
>>> dataset.expand_dims(dim="time") <xarray.Dataset> Size: 8B Dimensions: (time: 1) Dimensions without coordinates: time Data variables: temperature (time) float64 8B 25.0
# 1D data
>>> temperature_1d = xr.DataArray([25.0, 26.5, 24.8], dims="x") >>> dataset_1d = xr.Dataset({"temperature": temperature_1d}) >>> dataset_1d <xarray.Dataset> Size: 24B Dimensions: (x: 3) Dimensions without coordinates: x Data variables: temperature (x) float64 24B 25.0 26.5 24.8
# Expand the dataset with a new dimension called “time” using axis argument
>>> dataset_1d.expand_dims(dim="time", axis=0) <xarray.Dataset> Size: 24B Dimensions: (time: 1, x: 3) Dimensions without coordinates: time, x Data variables: temperature (time, x) float64 24B 25.0 26.5 24.8
# 2D data
>>> temperature_2d = xr.DataArray(np.random.rand(3, 4), dims=("y", "x")) >>> dataset_2d = xr.Dataset({"temperature": temperature_2d}) >>> dataset_2d <xarray.Dataset> Size: 96B Dimensions: (y: 3, x: 4) Dimensions without coordinates: y, x Data variables: temperature (y, x) float64 96B 0.5488 0.7152 0.6028 ... 0.7917 0.5289
# Expand the dataset with a new dimension called “time” using axis argument
>>> dataset_2d.expand_dims(dim="time", axis=2) <xarray.Dataset> Size: 96B Dimensions: (y: 3, x: 4, time: 1) Dimensions without coordinates: y, x, time Data variables: temperature (y, x, time) float64 96B 0.5488 0.7152 0.6028 ... 0.7917 0.5289
# Expand a scalar variable along a new dimension of the same name with and without creating a new index
>>> ds = xr.Dataset(coords={"x": 0}) >>> ds <xarray.Dataset> Size: 8B Dimensions: () Coordinates: x int64 8B 0 Data variables: *empty*
>>> ds.expand_dims("x") <xarray.Dataset> Size: 8B Dimensions: (x: 1) Coordinates: * x (x) int64 8B 0 Data variables: *empty*
>>> ds.expand_dims("x").indexes Indexes: x Index([0], dtype='int64', name='x')
>>> ds.expand_dims("x", create_index_for_new_dim=False).indexes Indexes: *empty*
See also