Skip to content

roll() now won't operate on multi-dimensional arrays.  #2445

Closed
@abrammer-climacell

Description

@abrammer-climacell

xarray now tries to roll all the coordinates, rather than the variable DataArrays and only the specified coordinate. This fails as the other coordinates do not have the specified coordinate and there is no check so we just get a ValueError

unrolled_vars = () if roll_coords else self.coords

This seems like a significant regression from 0.10.8, making roll on an nd dataset/array currently not possible, unless I'm missing something.


Code Sample

import xarray as xr
print(xr.__version__)
arr = xr.DataArray([[1, 2, 3],[4, 5, 6]], coords={'x': range(3), 'y': range(2)}, dims=('y','x'))
actual = arr.roll(x=1)
expected = xr.DataArray([[3, 1, 2],[6, 4, 5]], coords=[('y', [0, 1]), ('x', [2, 0, 1])])
actual == expected

0.10.8
<xarray.DataArray (y: 2, x: 3)>
array([[ True, True, True],
[ True, True, True]])
Coordinates:

  • x (x) int64 2 0 1
  • y (y) int64 0 1

If we try it on 0.10.9 -->
we get

ValueError: 'x' not found in array dimensions ('y',)

Full traceback nested below,

0.10.9


ValueError Traceback (most recent call last)
~/conda/envs/daily/lib/python3.6/site-packages/xarray/core/common.py in _get_axis_num(self, dim)
135 try:
--> 136 return self.dims.index(dim)
137 except ValueError:

ValueError: tuple.index(x): x not in tuple

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
in ()
2 print(xr.version)
3 arr = xr.DataArray([[1, 2, 3],[4, 5, 6]], coords={'x': range(3), 'y': range(2)}, dims=('y','x'))
----> 4 actual = arr.roll(x=1)
5 expected = xr.DataArray([[3, 1, 2],[6, 4, 5]], coords=[('y', [0, 1]), ('x', [2, 0, 1])])
6 actual == expected

~/conda/envs/daily/lib/python3.6/site-packages/xarray/core/dataarray.py in roll(self, shifts, roll_coords, **shifts_kwargs)
2159 """
2160 ds = self._to_temp_dataset().roll(
-> 2161 shifts=shifts, roll_coords=roll_coords, **shifts_kwargs)
2162 return self._from_temp_dataset(ds)
2163

~/conda/envs/daily/lib/python3.6/site-packages/xarray/core/dataset.py in roll(self, shifts, roll_coords, **shifts_kwargs)
3574 for k, v in iteritems(self.variables):
3575 if k not in unrolled_vars:
-> 3576 variables[k] = v.roll(**shifts)
3577 else:
3578 variables[k] = v

~/conda/envs/daily/lib/python3.6/site-packages/xarray/core/variable.py in roll(self, shifts, **shifts_kwargs)
1093 result = self
1094 for dim, count in shifts.items():
-> 1095 result = result._roll_one_dim(dim, count)
1096 return result
1097

~/conda/envs/daily/lib/python3.6/site-packages/xarray/core/variable.py in _roll_one_dim(self, dim, count)
1049
1050 def _roll_one_dim(self, dim, count):
-> 1051 axis = self.get_axis_num(dim)
1052
1053 count %= self.shape[axis]

~/conda/envs/daily/lib/python3.6/site-packages/xarray/core/common.py in get_axis_num(self, dim)
128 """
129 if isinstance(dim, basestring):
--> 130 return self._get_axis_num(dim)
131 else:
132 return tuple(self._get_axis_num(d) for d in dim)

~/conda/envs/daily/lib/python3.6/site-packages/xarray/core/common.py in _get_axis_num(self, dim)
137 except ValueError:
138 raise ValueError("%r not found in array dimensions %r" %
--> 139 (dim, self.dims))
140
141 @Property

ValueError: 'x' not found in array dimensions ('y',)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions