erlab.analysis.utilities¶
Functions
|
|
|
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 ofshift
must be a subset of the dimensions ofdarr
. For more information, see the note below. Ifshift
is afloat
, the same shift is applied to all values along dimensionalong
. 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. IfFalse
, the coordinates and shape of the original data will be retained, and only the data will be shifted. Defaults toFalse
.**shift_kwargs – Additional keyword arguments passed onto
scipy.ndimage.shift
. Default values ofcval
andorder
are set tonp.nan
and1
respectively.
- Returns:
The shifted DataArray.
- Return type:
Note
All dimensions in
shift
must be a dimension indarr
.The
shift
array values are divided by the step size along thealong
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