-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fill_value in shift #2470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fill_value in shift #2470
Changes from 5 commits
21ec3e4
d79d132
00e16ec
404ed82
03587ac
aaf1890
6469b5a
b9a112c
7feec29
9a0ea70
6ecf359
048e287
bb8ad25
31a7048
23f60b6
1243a52
fccfe4d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2080,7 +2080,7 @@ def diff(self, dim, n=1, label='upper'): | |
ds = self._to_temp_dataset().diff(n=n, dim=dim, label=label) | ||
return self._from_temp_dataset(ds) | ||
|
||
def shift(self, shifts=None, **shifts_kwargs): | ||
def shift(self, shifts=None, fill_value=None, **shifts_kwargs): | ||
"""Shift this array by an offset along one or more dimensions. | ||
|
||
Only the data is moved; coordinates stay in place. Values shifted from | ||
|
@@ -2093,6 +2093,8 @@ def shift(self, shifts=None, **shifts_kwargs): | |
Integer offset to shift along each of the given dimensions. | ||
Positive offsets shift to the right; negative offsets shift to the | ||
left. | ||
fill_value: scalar, optional | ||
Value to use for newly missing values | ||
**shifts_kwargs: | ||
The keyword arguments form of ``shifts``. | ||
One of shifts or shifts_kwarg must be provided. | ||
|
@@ -2117,8 +2119,8 @@ def shift(self, shifts=None, **shifts_kwargs): | |
Coordinates: | ||
* x (x) int64 0 1 2 | ||
""" | ||
ds = self._to_temp_dataset().shift(shifts=shifts, **shifts_kwargs) | ||
return self._from_temp_dataset(ds) | ||
return self._replace(variable=self.variable.shift( | ||
shifts=shifts, fill_value=fill_value, **shifts_kwargs)) | ||
|
||
|
||
def roll(self, shifts=None, roll_coords=None, **shifts_kwargs): | ||
"""Roll this array by an offset along one or more dimensions. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -929,7 +929,7 @@ def squeeze(self, dim=None): | |
dims = common.get_squeeze_dims(self, dim) | ||
return self.isel({d: 0 for d in dims}) | ||
|
||
def _shift_one_dim(self, dim, count): | ||
def _shift_one_dim(self, dim, count, fill_value=None): | ||
axis = self.get_axis_num(dim) | ||
|
||
if count > 0: | ||
|
@@ -940,7 +940,11 @@ def _shift_one_dim(self, dim, count): | |
keep = slice(None) | ||
|
||
trimmed_data = self[(slice(None),) * axis + (keep,)].data | ||
dtype, fill_value = dtypes.maybe_promote(self.dtype) | ||
|
||
if fill_value is None or fill_value is np.nan: | ||
|
||
dtype, fill_value = dtypes.maybe_promote(self.dtype) | ||
else: | ||
dtype = self.dtype | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if filler is not compatible with self.dtype? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In theory, NumPy should raise an error... But it may not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (this is the issue I'm looking at ref #2470 (comment)), good foresight @fujiisoup ! |
||
|
||
shape = list(self.shape) | ||
shape[axis] = min(abs(count), shape[axis]) | ||
|
@@ -952,12 +956,12 @@ def _shift_one_dim(self, dim, count): | |
else: | ||
full = np.full | ||
|
||
nans = full(shape, fill_value, dtype=dtype) | ||
filler = full(shape, fill_value, dtype=dtype) | ||
|
||
if count > 0: | ||
arrays = [nans, trimmed_data] | ||
arrays = [filler, trimmed_data] | ||
else: | ||
arrays = [trimmed_data, nans] | ||
arrays = [trimmed_data, filler] | ||
|
||
data = duck_array_ops.concatenate(arrays, axis) | ||
|
||
|
@@ -969,7 +973,7 @@ def _shift_one_dim(self, dim, count): | |
|
||
return type(self)(self.dims, data, self._attrs, fastpath=True) | ||
|
||
def shift(self, shifts=None, **shifts_kwargs): | ||
def shift(self, shifts=None, fill_value=None, **shifts_kwargs): | ||
""" | ||
Return a new Variable with shifted data. | ||
|
||
|
@@ -979,6 +983,8 @@ def shift(self, shifts=None, **shifts_kwargs): | |
Integer offset to shift along each of the given dimensions. | ||
Positive offsets shift to the right; negative offsets shift to the | ||
left. | ||
fill_value: scalar, optional | ||
Value to use for newly missing values | ||
**shifts_kwargs: | ||
The keyword arguments form of ``shifts``. | ||
One of shifts or shifts_kwarg must be provided. | ||
|
@@ -991,7 +997,7 @@ def shift(self, shifts=None, **shifts_kwargs): | |
shifts = either_dict_or_kwargs(shifts, shifts_kwargs, 'shift') | ||
result = self | ||
for dim, count in shifts.items(): | ||
result = result._shift_one_dim(dim, count) | ||
result = result._shift_one_dim(dim, count, fill_value=fill_value) | ||
return result | ||
|
||
def pad_with_fill_value(self, pad_widths=None, fill_value=dtypes.NA, | ||
|
Uh oh!
There was an error while loading. Please reload this page.