Skip to content

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

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

Closed
abrammer-climacell opened this issue Sep 27, 2018 · 2 comments
Closed

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

abrammer-climacell opened this issue Sep 27, 2018 · 2 comments
Labels

Comments

@abrammer-climacell
Copy link

abrammer-climacell commented Sep 27, 2018

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',)

@fujiisoup fujiisoup mentioned this issue Sep 27, 2018
4 tasks
@fujiisoup
Copy link
Member

Thanks, @abrammer-climacell, for the bug report!
This is actually a regression after #2360.
Sent a PR #2446

@fujiisoup fujiisoup added the bug label Sep 27, 2018
@abrammer-climacell
Copy link
Author

Awesome. Thanks, grabbed your edit and that fixes it for my use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants