From 5de668648ba659b7b905f273f6c24aa58f8fb563 Mon Sep 17 00:00:00 2001 From: fujiisoup Date: Thu, 27 Sep 2018 17:58:15 +0200 Subject: [PATCH 1/2] fix:2445 --- doc/whats-new.rst | 4 ++++ xarray/core/dataset.py | 9 +++++---- xarray/tests/test_dataset.py | 10 ++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 67d0d548ec5..7cfe360ed94 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -39,6 +39,10 @@ Enhancements Bug fixes ~~~~~~~~~ +- ``xarray.DataArray.roll`` correctly handles multidimensional arrays. + (:issue:`2445`) + By `Keisuke Fujii `_. + .. _whats-new.0.10.9: diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 9cf304858a6..8f556955bf7 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -743,7 +743,7 @@ def copy(self, deep=False, data=None): Shallow copy versus deep copy >>> da = xr.DataArray(np.random.randn(2, 3)) - >>> ds = xr.Dataset({'foo': da, 'bar': ('x', [-1, 2])}, + >>> ds = xr.Dataset({'foo': da, 'bar': ('x', [-1, 2])}, coords={'x': ['one', 'two']}) >>> ds.copy() @@ -775,7 +775,7 @@ def copy(self, deep=False, data=None): foo (dim_0, dim_1) float64 7.0 0.3897 -1.862 -0.6091 -1.051 -0.3003 bar (x) int64 -1 2 - Changing the data using the ``data`` argument maintains the + Changing the data using the ``data`` argument maintains the structure of the original object, but with the new data. Original object is unaffected. @@ -826,7 +826,7 @@ def copy(self, deep=False, data=None): # skip __init__ to avoid costly validation return self._construct_direct(variables, self._coord_names.copy(), self._dims.copy(), self._attrs_copy(), - encoding=self.encoding) + encoding=self.encoding) def _subset_with_all_valid_coords(self, variables, coord_names, attrs): needed_dims = set() @@ -3573,7 +3573,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 f8fb9b98ac3..a4466853ea6 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -3900,6 +3900,16 @@ def test_shift(self): with raises_regex(ValueError, 'dimensions'): ds.shift(foo=123) + def test_shift_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_roll_coords(self): coords = {'bar': ('x', list('abc')), 'x': [-4, 3, 2]} attrs = {'meta': 'data'} From 6013cc2b3acf36524bcca839c21480a23e0a4bab Mon Sep 17 00:00:00 2001 From: fujiisoup Date: Thu, 27 Sep 2018 18:38:23 +0200 Subject: [PATCH 2/2] rename test_shift_multidim -> test_roll_multidim --- xarray/tests/test_dataset.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index a4466853ea6..34d521e35db 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -3900,16 +3900,6 @@ def test_shift(self): with raises_regex(ValueError, 'dimensions'): ds.shift(foo=123) - def test_shift_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_roll_coords(self): coords = {'bar': ('x', list('abc')), 'x': [-4, 3, 2]} attrs = {'meta': 'data'} @@ -3947,6 +3937,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)