# xarray.Dataset.expand_dims

#### Dataset.expand_dims(dim=None, axis=None, create_index_for_new_dim=True, \*\*dim_kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L4573-L4814)

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](../user-guide/terminology.html.md#term-name), [sequence](https://docs.python.org/3/glossary.html#term-sequence) of [hashable](../user-guide/terminology.html.md#term-name), [mapping](https://docs.python.org/3/glossary.html#term-mapping), or [`None`](https://docs.python.org/3/builtins/constants.html#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`](https://docs.python.org/3/builtins/functions.html#int), [sequence](https://docs.python.org/3/glossary.html#term-sequence) of [`int`](https://docs.python.org/3/builtins/functions.html#int), or [`None`](https://docs.python.org/3/builtins/constants.html#None), *default*: [`None`](https://docs.python.org/3/builtins/constants.html#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`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`True`](https://docs.python.org/3/builtins/constants.html#True)) – Whether to create new `PandasIndex` objects when the object being expanded contains scalar variables with names in `dim`.
  * **\*\*dim_kwargs** ([`int`](https://docs.python.org/3/builtins/functions.html#int) or [sequence](https://docs.python.org/3/glossary.html#term-sequence) or [`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – 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`](xarray.Dataset.html.md#xarray.Dataset)) – This object, but with additional dimension(s).

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('b12e311e-8b8f-43e0-8453-e462766763c9','e59c0698-c6be-4b28-92b4-f187b00db714','810c8f1b-f5bf-43af-9165-1ea1e078aad2','../lite/tree/../notebooks/index.html?path=1e7a7c67_2f03_4516_9fc7_74cceb80dad6.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> 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”

```pycon
>>> 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

```pycon
>>> 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

```pycon
>>> 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

```pycon
>>> 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

```pycon
>>> 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

```pycon
>>> ds = xr.Dataset(coords={"x": 0})
>>> ds
<xarray.Dataset> Size: 8B
Dimensions:  ()
Coordinates:
    x        int64 8B 0
Data variables:
    *empty*
```

```pycon
>>> ds.expand_dims("x")
<xarray.Dataset> Size: 8B
Dimensions:  (x: 1)
Coordinates:
  * x        (x) int64 8B 0
Data variables:
    *empty*
```

```pycon
>>> ds.expand_dims("x").indexes
Indexes:
    x        Index([0], dtype='int64', name='x')
```

```pycon
>>> ds.expand_dims("x", create_index_for_new_dim=False).indexes
Indexes:
    *empty*
```

<div id="810c8f1b-f5bf-43af-9165-1ea1e078aad2" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('b12e311e-8b8f-43e0-8453-e462766763c9','810c8f1b-f5bf-43af-9165-1ea1e078aad2')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('b12e311e-8b8f-43e0-8453-e462766763c9','810c8f1b-f5bf-43af-9165-1ea1e078aad2')">Open In Tab</button></div><div id="e59c0698-c6be-4b28-92b4-f187b00db714" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

#### SEE ALSO
[`DataArray.expand_dims`](xarray.DataArray.expand_dims.html.md#xarray.DataArray.expand_dims)
