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 : {"uppe
(
self,
dim: Hashable,
n: int = 1,
*,
label: Literal["upper", "lower"] = "upper",
)
| 7824 | self.variables[v].attrs = other.variables[v].attrs |
| 7825 | |
| 7826 | def diff( |
| 7827 | self, |
| 7828 | dim: Hashable, |
| 7829 | n: int = 1, |
| 7830 | *, |
| 7831 | label: Literal["upper", "lower"] = "upper", |
| 7832 | ) -> Self: |
| 7833 | """Calculate the n-th order discrete difference along given axis. |
| 7834 | |
| 7835 | Parameters |
| 7836 | ---------- |
| 7837 | dim : Hashable |
| 7838 | Dimension over which to calculate the finite difference. |
| 7839 | n : int, default: 1 |
| 7840 | The number of times values are differenced. |
| 7841 | label : {"upper", "lower"}, default: "upper" |
| 7842 | The new coordinate in dimension ``dim`` will have the |
| 7843 | values of either the minuend's or subtrahend's coordinate |
| 7844 | for values 'upper' and 'lower', respectively. |
| 7845 | |
| 7846 | Returns |
| 7847 | ------- |
| 7848 | difference : Dataset |
| 7849 | The n-th order finite difference of this object. |
| 7850 | |
| 7851 | Notes |
| 7852 | ----- |
| 7853 | `n` matches numpy's behavior and is different from pandas' first argument named |
| 7854 | `periods`. |
| 7855 | |
| 7856 | Examples |
| 7857 | -------- |
| 7858 | >>> ds = xr.Dataset({"foo": ("x", [5, 5, 6, 6])}) |
| 7859 | >>> ds.diff("x") |
| 7860 | <xarray.Dataset> Size: 24B |
| 7861 | Dimensions: (x: 3) |
| 7862 | Dimensions without coordinates: x |
| 7863 | Data variables: |
| 7864 | foo (x) int64 24B 0 1 0 |
| 7865 | >>> ds.diff("x", 2) |
| 7866 | <xarray.Dataset> Size: 16B |
| 7867 | Dimensions: (x: 2) |
| 7868 | Dimensions without coordinates: x |
| 7869 | Data variables: |
| 7870 | foo (x) int64 16B 1 -1 |
| 7871 | |
| 7872 | See Also |
| 7873 | -------- |
| 7874 | Dataset.differentiate |
| 7875 | """ |
| 7876 | if n == 0: |
| 7877 | return self |
| 7878 | if n < 0: |
| 7879 | raise ValueError(f"order `n` must be non-negative but got {n}") |
| 7880 | |
| 7881 | # prepare slices |
| 7882 | slice_start = {dim: slice(None, -1)} |
| 7883 | slice_end = {dim: slice(1, None)} |