# 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('3cf081ed-298c-4801-9f8b-498570d3a7bf','416edf4c-a9d9-45f5-8dbc-5e408d2c5264','8614352b-d995-46b5-bc02-fcc2686cdfe9','../lite/tree/../notebooks/index.html?path=9f2e98ca_457a_49cd_8ac8_db88fdf9dfb2.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="8614352b-d995-46b5-bc02-fcc2686cdfe9" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('3cf081ed-298c-4801-9f8b-498570d3a7bf','8614352b-d995-46b5-bc02-fcc2686cdfe9')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('3cf081ed-298c-4801-9f8b-498570d3a7bf','8614352b-d995-46b5-bc02-fcc2686cdfe9')">Open In Tab</button></div><div id="416edf4c-a9d9-45f5-8dbc-5e408d2c5264" 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)
