Shift this dataset by an offset along one or more dimensions. Only data variables are moved; coordinates stay in place. This is consistent with the behavior of ``shift`` in pandas. Values shifted from beyond array bounds will appear at one end of each dimension, whi
(
self,
shifts: Mapping[Any, int] | None = None,
fill_value: Any = xrdtypes.NA,
**shifts_kwargs: int,
)
| 7912 | return difference |
| 7913 | |
| 7914 | def shift( |
| 7915 | self, |
| 7916 | shifts: Mapping[Any, int] | None = None, |
| 7917 | fill_value: Any = xrdtypes.NA, |
| 7918 | **shifts_kwargs: int, |
| 7919 | ) -> Self: |
| 7920 | """Shift this dataset by an offset along one or more dimensions. |
| 7921 | |
| 7922 | Only data variables are moved; coordinates stay in place. This is |
| 7923 | consistent with the behavior of ``shift`` in pandas. |
| 7924 | |
| 7925 | Values shifted from beyond array bounds will appear at one end of |
| 7926 | each dimension, which are filled according to `fill_value`. For periodic |
| 7927 | offsets instead see `roll`. |
| 7928 | |
| 7929 | Parameters |
| 7930 | ---------- |
| 7931 | shifts : mapping of hashable to int |
| 7932 | Integer offset to shift along each of the given dimensions. |
| 7933 | Positive offsets shift to the right; negative offsets shift to the |
| 7934 | left. |
| 7935 | fill_value : scalar or dict-like, optional |
| 7936 | Value to use for newly missing values. If a dict-like, maps |
| 7937 | variable names (including coordinates) to fill values. |
| 7938 | **shifts_kwargs |
| 7939 | The keyword arguments form of ``shifts``. |
| 7940 | One of shifts or shifts_kwargs must be provided. |
| 7941 | |
| 7942 | Returns |
| 7943 | ------- |
| 7944 | shifted : Dataset |
| 7945 | Dataset with the same coordinates and attributes but shifted data |
| 7946 | variables. |
| 7947 | |
| 7948 | See Also |
| 7949 | -------- |
| 7950 | roll |
| 7951 | |
| 7952 | Examples |
| 7953 | -------- |
| 7954 | >>> ds = xr.Dataset({"foo": ("x", list("abcde"))}) |
| 7955 | >>> ds.shift(x=2) |
| 7956 | <xarray.Dataset> Size: 40B |
| 7957 | Dimensions: (x: 5) |
| 7958 | Dimensions without coordinates: x |
| 7959 | Data variables: |
| 7960 | foo (x) object 40B nan nan 'a' 'b' 'c' |
| 7961 | """ |
| 7962 | shifts = either_dict_or_kwargs(shifts, shifts_kwargs, "shift") |
| 7963 | invalid = tuple(k for k in shifts if k not in self.dims) |
| 7964 | if invalid: |
| 7965 | raise ValueError( |
| 7966 | f"Dimensions {invalid} not found in data dimensions {tuple(self.dims)}" |
| 7967 | ) |
| 7968 | |
| 7969 | variables = {} |
| 7970 | for name, var in self.variables.items(): |
| 7971 | if name in self.data_vars: |