Skip to content

Check that dimensions are all valid when using reindex #694

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

Merged
merged 1 commit into from
Jan 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion xray/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ def align(*objects, **kwargs):
% list(kwargs))

joined_indexes = _join_indexes(join, objects)
return tuple(obj.reindex(copy=copy, **joined_indexes) for obj in objects)
result = []
for obj in objects:
valid_indexers = dict((k, v) for k, v in joined_indexes.items()
if k in obj.dims)
result.append(obj.reindex(copy=copy, **valid_indexers))
return tuple(result)


def partial_align(*objects, **kwargs):
Expand Down
4 changes: 3 additions & 1 deletion xray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,10 @@ def reindex_like(self, other, method=None, tolerance=None, copy=True):
DataArray.reindex
align
"""
indexers = dict((k, v) for k, v in other.indexes.items()
if k in self.dims)
return self.reindex(method=method, tolerance=tolerance, copy=copy,
**other.indexes)
**indexers)

def reindex(self, method=None, tolerance=None, copy=True, **indexers):
"""Conform this object onto a new set of indexes, filling in
Expand Down
8 changes: 7 additions & 1 deletion xray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,8 +1152,10 @@ def reindex_like(self, other, method=None, tolerance=None, copy=True):
Dataset.reindex
align
"""
indexers = dict((k, v) for k, v in other.indexes.items()
if k in self.dims)
return self.reindex(method=method, copy=copy, tolerance=tolerance,
**other.indexes)
**indexers)

def reindex(self, indexers=None, method=None, tolerance=None, copy=True, **kw_indexers):
"""Conform this object onto a new set of indexes, filling in
Expand Down Expand Up @@ -1203,6 +1205,10 @@ def reindex(self, indexers=None, method=None, tolerance=None, copy=True, **kw_in
# shortcut
return self.copy(deep=True) if copy else self

bad_dims = [d for d in indexers if d not in self.dims]
if bad_dims:
raise ValueError('invalid reindex dimensions: %s' % bad_dims)

variables = alignment.reindex_variables(
self.variables, self.indexes, indexers, method, tolerance, copy=copy)
return self._replace_vars_and_dims(variables)
Expand Down
7 changes: 5 additions & 2 deletions xray/core/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,11 @@ def _reindex_variables_against(variables, indexes, copy=False):
"""Reindex all DataArrays in the provided dict, leaving other values alone.
"""
alignable = [k for k, v in variables.items() if hasattr(v, 'indexes')]
aligned = [variables[a].reindex(copy=copy, **indexes)
for a in alignable]
aligned = []
for a in alignable:
valid_indexes = dict((k, v) for k, v in indexes.items()
if k in variables[a].dims)
aligned.append(variables[a].reindex(copy=copy, **valid_indexes))
new_variables = OrderedDict(variables)
new_variables.update(zip(alignable, aligned))
return new_variables
Expand Down
4 changes: 4 additions & 0 deletions xray/test/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,10 @@ def test_reindex(self):
with self.assertRaisesRegexp(ValueError, 'dictionary'):
data.reindex('foo')

# invalid dimension
with self.assertRaisesRegexp(ValueError, 'invalid reindex dim'):
data.reindex(invalid=0)

# out of order
expected = data.sel(dim1=data['dim1'][:10:-1])
actual = data.reindex(dim1=data['dim1'][:10:-1])
Expand Down