Skip to content

Commit 23d1cda

Browse files
authored
fix:2445 (#2446)
* fix:2445 * rename test_shift_multidim -> test_roll_multidim
1 parent c2b09d6 commit 23d1cda

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ Enhancements
5656
Bug fixes
5757
~~~~~~~~~
5858

59+
- ``xarray.DataArray.roll`` correctly handles multidimensional arrays.
60+
(:issue:`2445`)
61+
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
62+
5963
- ``xarray.DataArray.std()`` now correctly accepts ``ddof`` keyword argument.
6064
(:issue:`2240`)
6165
By `Keisuke Fujii <https://github.com/fujiisoup>`_.

xarray/core/dataset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3595,7 +3595,8 @@ def roll(self, shifts=None, roll_coords=None, **shifts_kwargs):
35953595
variables = OrderedDict()
35963596
for k, v in iteritems(self.variables):
35973597
if k not in unrolled_vars:
3598-
variables[k] = v.roll(**shifts)
3598+
variables[k] = v.roll(**{k: s for k, s in shifts.items()
3599+
if k in v.dims})
35993600
else:
36003601
variables[k] = v
36013602

xarray/tests/test_dataset.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3945,6 +3945,16 @@ def test_roll_coords_none(self):
39453945
expected = Dataset({'foo': ('x', [3, 1, 2])}, ex_coords, attrs)
39463946
assert_identical(expected, actual)
39473947

3948+
def test_roll_multidim(self):
3949+
# regression test for 2445
3950+
arr = xr.DataArray(
3951+
[[1, 2, 3],[4, 5, 6]], coords={'x': range(3), 'y': range(2)},
3952+
dims=('y','x'))
3953+
actual = arr.roll(x=1, roll_coords=True)
3954+
expected = xr.DataArray([[3, 1, 2],[6, 4, 5]],
3955+
coords=[('y', [0, 1]), ('x', [2, 0, 1])])
3956+
assert_identical(expected, actual)
3957+
39483958
def test_real_and_imag(self):
39493959
attrs = {'foo': 'bar'}
39503960
ds = Dataset({'x': ((), 1 + 2j, attrs)}, attrs=attrs)

0 commit comments

Comments
 (0)