Skip to content
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

Merged
merged 10 commits into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ Enhancements
(:issue:`2331`)
By `Maximilian Roos <https://github.com/max-sixty>`_.

- You can now control whether or not to offset the coordinates when using
the ``roll`` method and the current behavior, coordinates rolled by default,
raises a deprecation warning unless explicitly setting the keyword argument.
(:issue:`1875`)
By `Andrew Huang <https://github.com/ahuang11>`_.


Bug fixes
~~~~~~~~~
Expand Down
14 changes: 10 additions & 4 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Member

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:

indexers = either_dict_or_kwargs(indexers, indexers_kwargs, 'isel')

Copy link
Contributor Author

@ahuang11 ahuang11 Aug 13, 2018

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?)

"""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.
Copy link
Member

Choose a reason for hiding this comment

The 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.
Expand All @@ -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
Expand Down
25 changes: 19 additions & 6 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this logic should be updated.
All the variables in data_vars should be rolled, independent from roll_coords value.
Other variables (variables in self.coord_names) may depend on whether roll_coords is True or not.

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)

Expand Down
18 changes: 15 additions & 3 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3837,18 +3837,30 @@ def test_shift(self):
with raises_regex(ValueError, 'dimensions'):
ds.shift(foo=123)

def test_roll(self):
def test_roll_coords(self):
coords = {'bar': ('x', list('abc')), 'x': [-4, 3, 2]}
attrs = {'meta': 'data'}
ds = Dataset({'foo': ('x', [1, 2, 3])}, coords, attrs)
actual = ds.roll(x=1)
actual = ds.roll(x=1, roll_coords=True)

ex_coords = {'bar': ('x', list('cab')), 'x': [2, -4, 3]}
expected = Dataset({'foo': ('x', [3, 1, 2])}, ex_coords, attrs)
assert_identical(expected, actual)

with raises_regex(ValueError, 'dimensions'):
ds.roll(foo=123)
ds.roll(foo=123, roll_coords=True)

def test_roll_no_coords(self):
coords = {'bar': ('x', list('abc')), 'x': [-4, 3, 2]}
attrs = {'meta': 'data'}
ds = Dataset({'foo': ('x', [1, 2, 3])}, coords, attrs)
actual = ds.roll(x=1, roll_coords=False)

expected = Dataset({'foo': ('x', [3, 1, 2])}, coords, attrs)
assert_identical(expected, actual)

with raises_regex(ValueError, 'dimensions'):
ds.roll(abc=321, roll_coords=False)

def test_real_and_imag(self):
attrs = {'foo': 'bar'}
Expand Down