# 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('47f9a177-93d7-4ca9-b336-bd62c8c5d6a9','0447f9c3-ab8d-4625-8e96-c4b5d91d001b','5b32be89-058e-428b-85f6-27b344038c58','../lite/tree/../notebooks/index.html?path=611757c8_f0eb_4fe0_8484_e6d33405eebe.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="5b32be89-058e-428b-85f6-27b344038c58" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('47f9a177-93d7-4ca9-b336-bd62c8c5d6a9','5b32be89-058e-428b-85f6-27b344038c58')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('47f9a177-93d7-4ca9-b336-bd62c8c5d6a9','5b32be89-058e-428b-85f6-27b344038c58')">Open In Tab</button></div><div id="0447f9c3-ab8d-4625-8e96-c4b5d91d001b" 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)
