# xarray.DataArray.diff

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

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.

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

### 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('457f902f-74ab-40d9-848e-1a379bcf3fd4','41fc954b-d340-4549-a518-2470aa72a17a','8a62f277-b1a4-4373-ad56-21430af46283','../lite/tree/../notebooks/index.html?path=773a7d2e_83eb_4b0e_b3ee_0cdea569d66e.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="8a62f277-b1a4-4373-ad56-21430af46283" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('457f902f-74ab-40d9-848e-1a379bcf3fd4','8a62f277-b1a4-4373-ad56-21430af46283')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('457f902f-74ab-40d9-848e-1a379bcf3fd4','8a62f277-b1a4-4373-ad56-21430af46283')">Open In Tab</button></div><div id="41fc954b-d340-4549-a518-2470aa72a17a" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
