# xarray.Dataset.diff

#### Dataset.diff(dim, n=1, , label='upper')[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L8048-L8137)

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** ([`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – The n-th order finite difference of this object.

#### SEE ALSO
[`Dataset.differentiate`](xarray.Dataset.differentiate.html.md#xarray.Dataset.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('0f5c8ab5-4e03-4845-a407-76260f0e902c','ecaa7cd0-1293-4ed6-a90e-c6c996aa8bc0','aff8f684-1767-4080-a12a-4bf5c14c6f94','../lite/tree/../notebooks/index.html?path=c60606f4_6afc_4bb4_970d_6928b53b9595.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> ds = xr.Dataset({"foo": ("x", [5, 5, 6, 6])})
>>> ds.diff("x")
<xarray.Dataset> Size: 24B
Dimensions:  (x: 3)
Dimensions without coordinates: x
Data variables:
    foo      (x) int64 24B 0 1 0
>>> ds.diff("x", 2)
<xarray.Dataset> Size: 16B
Dimensions:  (x: 2)
Dimensions without coordinates: x
Data variables:
    foo      (x) int64 16B 1 -1
```

<div id="aff8f684-1767-4080-a12a-4bf5c14c6f94" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('0f5c8ab5-4e03-4845-a407-76260f0e902c','aff8f684-1767-4080-a12a-4bf5c14c6f94')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('0f5c8ab5-4e03-4845-a407-76260f0e902c','aff8f684-1767-4080-a12a-4bf5c14c6f94')">Open In Tab</button></div><div id="ecaa7cd0-1293-4ed6-a90e-c6c996aa8bc0" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
