MCPcopy Index your code
hub / github.com/pydata/xarray / _shift_one_dim

Method _shift_one_dim

xarray/core/variable.py:1166–1200  ·  view source on GitHub ↗
(self, dim, count, fill_value=dtypes.NA)

Source from the content-addressed store, hash-verified

1164 return self.isel(dict.fromkeys(dims, 0))
1165
1166 def _shift_one_dim(self, dim, count, fill_value=dtypes.NA):
1167 axis = self.get_axis_num(dim)
1168
1169 if count > 0:
1170 keep = slice(None, -count)
1171 elif count < 0:
1172 keep = slice(-count, None)
1173 else:
1174 keep = slice(None)
1175
1176 trimmed_data = self[(slice(None),) * axis + (keep,)].data
1177
1178 if fill_value is dtypes.NA:
1179 dtype, fill_value = dtypes.maybe_promote(self.dtype)
1180 else:
1181 dtype = self.dtype
1182
1183 width = min(abs(count), self.shape[axis])
1184 dim_pad = (width, 0) if count >= 0 else (0, width)
1185 pads = [(0, 0) if d != dim else dim_pad for d in self.dims]
1186
1187 data = duck_array_ops.pad(
1188 duck_array_ops.astype(trimmed_data, dtype),
1189 pads,
1190 mode="constant",
1191 constant_values=fill_value,
1192 )
1193
1194 if is_duck_dask_array(data):
1195 # chunked data should come out with the same chunks; this makes
1196 # it feasible to combine shifted and unshifted data
1197 # TODO: remove this once dask.array automatically aligns chunks
1198 data = data.rechunk(self.data.chunks)
1199
1200 return self._replace(data=data)
1201
1202 def shift(self, shifts=None, fill_value=dtypes.NA, **shifts_kwargs):
1203 """

Callers 1

shiftMethod · 0.80

Calls 6

_replaceMethod · 0.95
is_duck_dask_arrayFunction · 0.90
get_axis_numMethod · 0.45
padMethod · 0.45
astypeMethod · 0.45
rechunkMethod · 0.45

Tested by

no test coverage detected