🍾 Xarray is now 10 years old! πŸŽ‰

xarray.DataArray.differentiate

xarray.DataArray.differentiate#

DataArray.differentiate(coord, edge_order=1, datetime_unit=None)[source]#

Differentiate the array with the second order accurate central differences.

Note

This feature is limited to simple cartesian geometry, i.e. coord must be one dimensional.

Parameters:
  • coord (Hashable) – The coordinate to be used to compute the gradient.

  • edge_order ({1, 2}, default: 1) – N-th order accurate differences at the boundaries.

  • datetime_unit ({"W", "D", "h", "m", "s", "ms", "us", "ns", "ps", "fs", "as", None}, optional) – Unit to compute gradient. Only valid for datetime coordinate. β€œY” and β€œM” are not available as datetime_unit.

Returns:

differentiated (DataArray)

See also

numpy.gradient

corresponding numpy function

Examples

>>> da = xr.DataArray(
...     np.arange(12).reshape(4, 3),
...     dims=["x", "y"],
...     coords={"x": [0, 0.1, 1.1, 1.2]},
... )
>>> da
<xarray.DataArray (x: 4, y: 3)> Size: 96B
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
Coordinates:
  * x        (x) float64 32B 0.0 0.1 1.1 1.2
Dimensions without coordinates: y
>>>
>>> da.differentiate("x")
<xarray.DataArray (x: 4, y: 3)> Size: 96B
array([[30.        , 30.        , 30.        ],
       [27.54545455, 27.54545455, 27.54545455],
       [27.54545455, 27.54545455, 27.54545455],
       [30.        , 30.        , 30.        ]])
Coordinates:
  * x        (x) float64 32B 0.0 0.1 1.1 1.2
Dimensions without coordinates: y