# xarray.DataArray.expand_dims

#### DataArray.expand_dims(dim=None, axis=None, create_index_for_new_dim=True, \*\*dim_kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataarray.py#L2792-L2892)

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](https://docs.python.org/3/glossary.html#term-sequence) of `Hashable`, [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict), or [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – Dimensions to include on the new variable.
    If provided as str or sequence of str, then dimensions are inserted
    with length 1. If provided as a dict, then the keys are the new
    dimensions and the values are either integers (giving the length of
    the new dimensions) or sequence/ndarray (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** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – This object, but with additional dimension(s).

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

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('1adc8ada-3ec9-44da-81c2-a1c0d970a7d5','183cfb0b-99a5-4b59-b458-b02e3626cb49','762d91e8-0d1a-4f1a-ade5-43b2ed4436b8','../lite/tree/../notebooks/index.html?path=0da15b37_000a_4216_b644_ec4be632059d.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> da = xr.DataArray(np.arange(5), dims=("x"))
>>> da
<xarray.DataArray (x: 5)> Size: 40B
array([0, 1, 2, 3, 4])
Dimensions without coordinates: x
```

Add new dimension of length 2:

```pycon
>>> da.expand_dims(dim={"y": 2})
<xarray.DataArray (y: 2, x: 5)> Size: 80B
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])
Dimensions without coordinates: y, x
```

```pycon
>>> da.expand_dims(dim={"y": 2}, axis=1)
<xarray.DataArray (x: 5, y: 2)> Size: 80B
array([[0, 0],
       [1, 1],
       [2, 2],
       [3, 3],
       [4, 4]])
Dimensions without coordinates: x, y
```

Add a new dimension with coordinates from array:

```pycon
>>> da.expand_dims(dim={"y": np.arange(5)}, axis=0)
<xarray.DataArray (y: 5, x: 5)> Size: 200B
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])
Coordinates:
  * y        (y) int64 40B 0 1 2 3 4
Dimensions without coordinates: x
```

<div id="762d91e8-0d1a-4f1a-ade5-43b2ed4436b8" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('1adc8ada-3ec9-44da-81c2-a1c0d970a7d5','762d91e8-0d1a-4f1a-ade5-43b2ed4436b8')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('1adc8ada-3ec9-44da-81c2-a1c0d970a7d5','762d91e8-0d1a-4f1a-ade5-43b2ed4436b8')">Open In Tab</button></div><div id="183cfb0b-99a5-4b59-b458-b02e3626cb49" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
