xarray.Dataset.diff

Contents

xarray.Dataset.diff#

Dataset.diff(dim, n=1, *, label='upper')[source]#

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

Parameters:
  • dim (Hashable) – Dimension over which to calculate the finite difference.

  • n (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) – 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

>>> 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