# 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#L2351-L2478)

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('2804a725-45e2-4c20-9c12-53ee29873f7c','0303b100-d028-46fb-9963-73c4e3ff39c7','7d18224e-422b-4543-81f9-328073a93736','../lite/tree/../notebooks/index.html?path=90752407_dfcf_4022_8970_e4fe6f7b7477.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="7d18224e-422b-4543-81f9-328073a93736" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('2804a725-45e2-4c20-9c12-53ee29873f7c','7d18224e-422b-4543-81f9-328073a93736')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('2804a725-45e2-4c20-9c12-53ee29873f7c','7d18224e-422b-4543-81f9-328073a93736')">Open In Tab</button></div><div id="0303b100-d028-46fb-9963-73c4e3ff39c7" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
