diff --git a/doc/whats-new.rst b/doc/whats-new.rst index a5b7b36142e..8b145924f2d 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -56,6 +56,10 @@ Enhancements Bug fixes ~~~~~~~~~ +- ``xarray.DataArray.roll`` correctly handles multidimensional arrays. + (:issue:`2445`) + By `Keisuke Fujii `_. + - ``xarray.DataArray.std()`` now correctly accepts ``ddof`` keyword argument. (:issue:`2240`) By `Keisuke Fujii `_. diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 4ad5902ebad..5e787c1587b 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -3595,7 +3595,8 @@ def roll(self, shifts=None, roll_coords=None, **shifts_kwargs): variables = OrderedDict() for k, v in iteritems(self.variables): if k not in unrolled_vars: - variables[k] = v.roll(**shifts) + variables[k] = v.roll(**{k: s for k, s in shifts.items() + if k in v.dims}) else: variables[k] = v diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index c42b84c05fc..2c964b81b98 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -3945,6 +3945,16 @@ def test_roll_coords_none(self): expected = Dataset({'foo': ('x', [3, 1, 2])}, ex_coords, attrs) assert_identical(expected, actual) + def test_roll_multidim(self): + # regression test for 2445 + arr = xr.DataArray( + [[1, 2, 3],[4, 5, 6]], coords={'x': range(3), 'y': range(2)}, + dims=('y','x')) + actual = arr.roll(x=1, roll_coords=True) + expected = xr.DataArray([[3, 1, 2],[6, 4, 5]], + coords=[('y', [0, 1]), ('x', [2, 0, 1])]) + assert_identical(expected, actual) + def test_real_and_imag(self): attrs = {'foo': 'bar'} ds = Dataset({'x': ((), 1 + 2j, attrs)}, attrs=attrs)