# xarray.DataArray.diff

#### DataArray.diff(dim, n=1, , label='upper')[[source]](https://github.com/pydata/xarray/blob/v2026.09.0/xarray/core/dataarray.py#L5112-L5161)

Calculate the n-th order discrete difference along given axis.

* **Parameters:**
  * **dim** (`Hashable`) – Dimension over which to calculate the finite difference.
  * **n** ([`int`](https://docs.python.org/3/builtins/functions.html#int), *default*: `1`) – The number of times values are differenced.
  * **label** (`{"upper", "lower"}`, *default*: `"upper"`) – The new coordinate in dimension `dim` will have the
    values of either the minuend’s or subtrahend’s coordinate
    for values ‘upper’ and ‘lower’, respectively.
* **Returns:**
  **difference** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – The n-th order finite difference of this object.

### Notes

n matches numpy’s behavior and is different from pandas’ first argument named
periods.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('a4a5d0f1-f2a9-4405-be5f-17d1b386493e','d0c7762b-daa3-4f07-826f-7699933b844f','454aee99-c02b-4dff-85f4-0a224009d720','../lite/tree/../notebooks/index.html?path=b461ccc9_28a4_4226_92e9_dee26115df54.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> arr = xr.DataArray([5, 5, 6, 6], [[1, 2, 3, 4]], ["x"])
>>> arr.diff("x")
<xarray.DataArray (x: 3)> Size: 24B
array([0, 1, 0])
Coordinates:
  * x        (x) int64 24B 2 3 4
>>> arr.diff("x", 2)
<xarray.DataArray (x: 2)> Size: 16B
array([ 1, -1])
Coordinates:
  * x        (x) int64 16B 3 4
```

<div id="454aee99-c02b-4dff-85f4-0a224009d720" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('a4a5d0f1-f2a9-4405-be5f-17d1b386493e','454aee99-c02b-4dff-85f4-0a224009d720')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('a4a5d0f1-f2a9-4405-be5f-17d1b386493e','454aee99-c02b-4dff-85f4-0a224009d720')">Open In Tab</button></div><div id="d0c7762b-daa3-4f07-826f-7699933b844f" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

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