# xarray.Dataset.diff

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

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.

### 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('421ef6ff-2f8d-4619-85b0-7889ddd06c3d','0a29d3a1-8c66-46f4-9fd0-21fecad2be83','5cfd4617-b28b-47a0-a933-e8b4da9a44f1','../lite/tree/../notebooks/index.html?path=7d7d727b_2080_44bb_8676_81a19ca1de85.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="5cfd4617-b28b-47a0-a933-e8b4da9a44f1" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('421ef6ff-2f8d-4619-85b0-7889ddd06c3d','5cfd4617-b28b-47a0-a933-e8b4da9a44f1')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('421ef6ff-2f8d-4619-85b0-7889ddd06c3d','5cfd4617-b28b-47a0-a933-e8b4da9a44f1')">Open In Tab</button></div><div id="0a29d3a1-8c66-46f4-9fd0-21fecad2be83" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

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