# xarray.IndexVariable.rolling_window

#### IndexVariable.rolling_window(dim, window, window_dim, \*, center=False, fill_value=<NA>, \*\*kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/variable.py#L2357-L2485)

Make a rolling_window along dim and add a new_dim to the last place.

* **Parameters:**
  * **dim** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str)) – Dimension over which to compute rolling_window.
    For nd-rolling, should be list of dimensions.
  * **window** ([`int`](https://docs.python.org/3/builtins/functions.html#int)) – Window size of the rolling
    For nd-rolling, should be list of integers.
  * **window_dim** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str)) – New name of the window dimension.
    For nd-rolling, should be list of strings.
  * **center** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`False`](https://docs.python.org/3/builtins/constants.html#False)) – If True, pad fill_value for both ends. Otherwise, pad in the head
    of the axis.
  * **fill_value** – value to be filled.
  * **\*\*kwargs** – Keyword arguments that should be passed to the underlying array type’s
    `sliding_window_view` function.
* **Returns:**
  * `Variable that is a view` of `the original array with an added dimension of`
  * `size w.`
  * **The return dim** (`self.dims + (window_dim`, `)`)
  * **The return shape** (`self.shape + (window`, `)`)

#### SEE ALSO
[`numpy.lib.stride_tricks.sliding_window_view`](https://numpy.org/doc/stable/reference/generated/numpy.lib.stride_tricks.sliding_window_view.html#numpy.lib.stride_tricks.sliding_window_view), [`dask.array.lib.stride_tricks.sliding_window_view`](https://docs.dask.org/en/latest/generated/dask.array.lib.stride_tricks.sliding_window_view.html#dask.array.lib.stride_tricks.sliding_window_view)

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('4ca70509-8a21-4920-856d-b9f336be7aea','43ed110a-d662-409e-9916-8f4cc1f7a01e','b838dde3-618a-461b-9fff-0a91aa321633','../lite/tree/../notebooks/index.html?path=c4b69a27_6176_43cb_afef_0d807c76bab3.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> v = Variable(("a", "b"), np.arange(8).reshape((2, 4)))
>>> v.rolling_window("b", 3, "window_dim")
<xarray.Variable (a: 2, b: 4, window_dim: 3)> Size: 192B
array([[[nan, nan,  0.],
        [nan,  0.,  1.],
        [ 0.,  1.,  2.],
        [ 1.,  2.,  3.]],

       [[nan, nan,  4.],
        [nan,  4.,  5.],
        [ 4.,  5.,  6.],
        [ 5.,  6.,  7.]]])
```

```pycon
>>> v.rolling_window("b", 3, "window_dim", center=True)
<xarray.Variable (a: 2, b: 4, window_dim: 3)> Size: 192B
array([[[nan,  0.,  1.],
        [ 0.,  1.,  2.],
        [ 1.,  2.,  3.],
        [ 2.,  3., nan]],

       [[nan,  4.,  5.],
        [ 4.,  5.,  6.],
        [ 5.,  6.,  7.],
        [ 6.,  7., nan]]])
```

<div id="b838dde3-618a-461b-9fff-0a91aa321633" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('4ca70509-8a21-4920-856d-b9f336be7aea','b838dde3-618a-461b-9fff-0a91aa321633')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('4ca70509-8a21-4920-856d-b9f336be7aea','b838dde3-618a-461b-9fff-0a91aa321633')">Open In Tab</button></div><div id="43ed110a-d662-409e-9916-8f4cc1f7a01e" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
