# xarray.DataArray.diff

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

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('ea8003c1-9fab-4c4e-bc94-4572c4eb7b7c','dd219f0f-1d1c-41c1-92cf-788d0edd6836','04fe5f6e-cf84-46aa-9a1d-e3f296801872','../lite/tree/../notebooks/index.html?path=20608058_0a31_447d_8493_2e77de82a56e.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="04fe5f6e-cf84-46aa-9a1d-e3f296801872" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('ea8003c1-9fab-4c4e-bc94-4572c4eb7b7c','04fe5f6e-cf84-46aa-9a1d-e3f296801872')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('ea8003c1-9fab-4c4e-bc94-4572c4eb7b7c','04fe5f6e-cf84-46aa-9a1d-e3f296801872')">Open In Tab</button></div><div id="dd219f0f-1d1c-41c1-92cf-788d0edd6836" 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)
