erlab.analysis.utilities

Functions

correct_with_edge(*args, **kwargs)

shift(darr, shift, along[, shift_coords])

Shifts the values of a DataArray along a single dimension.

erlab.analysis.utilities.shift(darr, shift, along, shift_coords=False, **shift_kwargs)[source]

Shifts the values of a DataArray along a single dimension.

The shift is applied using scipy.ndimage.shift with the specified keyword arguments. Linear interpolation is used by default.

Parameters:
  • darr (DataArray) – The array to shift.

  • shift (float | DataArray) – The amount of shift to be applied along the specified dimension. If shift is a DataArray, different shifts can be applied to different coordinates. The dimensions of shift must be a subset of the dimensions of darr. For more information, see the note below. If shift is a float, the same shift is applied to all values along dimension along. This is equivalent to providing a 0-dimensional DataArray.

  • along (str) – Name of the dimension along which the shift is applied.

  • shift_coords (bool) – If True, the coordinates of the output data will be changed so that the output contains all the values of the original data. If False, the coordinates and shape of the original data will be retained, and only the data will be shifted. Defaults to False.

  • **shift_kwargs – Additional keyword arguments passed onto scipy.ndimage.shift. Default values of cval and order are set to np.nan and 1 respectively.

Returns:

The shifted DataArray.

Return type:

xarray.DataArray

Note

  • All dimensions in shift must be a dimension in darr.

  • The shift array values are divided by the step size along the along dimension.

  • NaN values in shift are treated as zero.

Example

>>> import xarray as xr
>>> darr = xr.DataArray(
...     np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).astype(float), dims=["x", "y"]
... )
>>> shift_arr = xr.DataArray([1, 0, 2], dims=["x"])
>>> shifted = erlab.analysis.utilities.shift(darr, shift_arr, along="y")
>>> print(shifted)
<xarray.DataArray (x: 3, y: 3)> Size: 72B
nan 1.0 2.0 4.0 5.0 6.0 nan nan 7.0
Dimensions without coordinates: x, y