-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add option to not roll coords #2360
Changes from 3 commits
2f8a835
2caa553
a2737b8
0b5e7fd
a4da1ca
896b18a
c64f726
5725578
a57f794
3f9dc4a
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 |
---|---|---|
|
@@ -2015,14 +2015,20 @@ def shift(self, **shifts): | |
variable = self.variable.shift(**shifts) | ||
return self._replace(variable) | ||
|
||
def roll(self, **shifts): | ||
def roll(self, roll_coords=None, **shifts): | ||
"""Roll this array by an offset along one or more dimensions. | ||
|
||
Unlike shift, roll rotates all variables, including coordinates. The | ||
direction of rotation is consistent with :py:func:`numpy.roll`. | ||
Unlike shift, roll may rotate all variables, including coordinates | ||
if specified. The direction of rotation is consistent with | ||
:py:func:`numpy.roll`. | ||
|
||
Parameters | ||
---------- | ||
roll_coords : bool | ||
Indicates whether to roll the coordinates by the offset | ||
The current default of roll_coords (None, equivalent to True) is | ||
deprecated and will change to False in a future version. | ||
Explicitly pass roll_coords to silence the warning and sort. | ||
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 does "and sort" refer to? |
||
**shifts : keyword arguments of the form {dim: offset} | ||
Integer offset to rotate each of the given dimensions. Positive | ||
offsets roll to the right; negative offsets roll to the left. | ||
|
@@ -2046,7 +2052,7 @@ def roll(self, **shifts): | |
Coordinates: | ||
* x (x) int64 2 0 1 | ||
""" | ||
ds = self._to_temp_dataset().roll(**shifts) | ||
ds = self._to_temp_dataset().roll(roll_coords=roll_coords, **shifts) | ||
return self._from_temp_dataset(ds) | ||
|
||
@property | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3355,14 +3355,20 @@ def shift(self, **shifts): | |
|
||
return self._replace_vars_and_dims(variables) | ||
|
||
def roll(self, **shifts): | ||
def roll(self, roll_coords=None, **shifts): | ||
"""Roll this dataset by an offset along one or more dimensions. | ||
|
||
Unlike shift, roll rotates all variables, including coordinates. The | ||
direction of rotation is consistent with :py:func:`numpy.roll`. | ||
Unlike shift, roll may rotate all variables, including coordinates | ||
if specified. The direction of rotation is consistent with | ||
:py:func:`numpy.roll`. | ||
|
||
Parameters | ||
---------- | ||
roll_coords : bool | ||
Indicates whether to roll the coordinates by the offset | ||
The current default of roll_coords (None, equivalent to True) is | ||
deprecated and will change to False in a future version. | ||
Explicitly pass roll_coords to silence the warning and sort. | ||
**shifts : keyword arguments of the form {dim: offset} | ||
Integer offset to rotate each of the given dimensions. Positive | ||
offsets roll to the right; negative offsets roll to the left. | ||
|
@@ -3395,9 +3401,16 @@ def roll(self, **shifts): | |
|
||
variables = OrderedDict() | ||
for name, var in iteritems(self.variables): | ||
var_shifts = dict((k, v) for k, v in shifts.items() | ||
if k in var.dims) | ||
variables[name] = var.roll(**var_shifts) | ||
if name in self.data_vars or roll_coords: | ||
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. I think this logic should be updated. |
||
if roll_coords is None: | ||
warnings.warn("roll_coords will be set to False in the future." | ||
" Explicitly set roll_coords to silence warning.", | ||
DeprecationWarning, stacklevel=3) | ||
var_shifts = dict((k, v) for k, v in shifts.items() | ||
if k in var.dims) | ||
variables[name] = var.roll(**var_shifts) | ||
else: | ||
variables[name] = var | ||
|
||
return self._replace_vars_and_dims(variables) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be nice to use
utils.either_dict_or_kwargs
here, to handle potential (unlikely) conflicts between the name "roll_coords" and dimension names.Example usage:
xarray/xarray/core/dataarray.py
Line 755 in ded0a68
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not too sure I understand this util; let me know if I implemented it inaccurately. (I think I got it now?)