xarray.Dataset.diff

Dataset.diff(self, dim, n=1, label='upper')

Calculate the n-th order discrete difference along given axis.

Parameters
  • dim (str, optional) – Dimension over which to calculate the finite difference.

  • n (int, optional) – The number of times values are differenced.

  • label (str, optional) – 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. Other values are not supported.

Returns

  • difference (same type as caller) – The n-th order finite difference of this object.

  • .. note::n matches numpy’s behavior and is different from pandas’ first argument named periods.

Examples

>>> ds = xr.Dataset({"foo": ("x", [5, 5, 6, 6])})
>>> ds.diff("x")
<xarray.Dataset>
Dimensions:  (x: 3)
Coordinates:
  * x        (x) int64 1 2 3
Data variables:
    foo      (x) int64 0 1 0
>>> ds.diff("x", 2)
<xarray.Dataset>
Dimensions:  (x: 2)
Coordinates:
* x        (x) int64 2 3
Data variables:
foo      (x) int64 1 -1