-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix in vectorized item assignment #1746
Changes from 1 commit
9eafe36
2aff4ce
22c1295
dc135cd
b1dd0f0
c74c828
8e6e2e0
aef3d56
f10ecf4
d482f80
c2b5ac3
84e5e6f
2011140
6906eeb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -301,3 +301,21 @@ def __getitem__(self, key): | |
|
||
def __unicode__(self): | ||
return formatting.indexes_repr(self) | ||
|
||
|
||
def assert_coordinate_consistent(obj, coords): | ||
""" Maeke sure the dimension coordinate of obj is | ||
consistent with coords. | ||
|
||
obj: DataArray or Dataset | ||
coords: Dict-like of variables | ||
""" | ||
for k in obj.dims: | ||
# make sure there are no conflict in dimension coordinates | ||
if (k in coords and k in obj.coords): | ||
coord = getattr(coords[k], 'variable', coords[k]) # Variable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to insist that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's really reasonable. Updated. |
||
if not coord.equals(obj[k].variable): | ||
raise IndexError( | ||
'dimension coordinate {!r} conflicts between ' | ||
'indexed and indexing objects:\n{}\nvs.\n{}' | ||
.format(k, obj[k], coords[k])) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
from .alignment import align, reindex_like_indexers | ||
from .common import AbstractArray, BaseDataObject | ||
from .coordinates import (DataArrayCoordinates, LevelCoordinatesSource, | ||
Indexes) | ||
Indexes, assert_coordinate_consistent) | ||
from .dataset import Dataset, merge_indexes, split_indexes | ||
from .pycompat import iteritems, basestring, OrderedDict, zip, range | ||
from .variable import (as_variable, Variable, as_compatible_data, | ||
|
@@ -484,6 +484,10 @@ def __setitem__(self, key, value): | |
if isinstance(key, basestring): | ||
self.coords[key] = value | ||
else: | ||
# Coordinates in key, value and self[key] should be consistent. | ||
obj = self[key] | ||
if isinstance(value, DataArray): | ||
assert_coordinate_consistent(value, obj.coords) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was actually thinking of checking the consistency of coords on each DataArray argument in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if you use |
||
# DataArray key -> Variable key | ||
key = {k: v.variable if isinstance(v, DataArray) else v | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to enforce consistency for coordinates here? My inclination would be that we should support exactly the same keys in setitem as are valid in getitem. Ideally we should also reuse the same code. That means we should raise errors if there are multiple indexers with inconsistent alignment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Reasonable. I will add a validation. |
||
for k, v in self._item_key_to_dict(key).items()} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to check that coordinates are consistent on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is done a few lines above, But I am wondering this unnecessary indexing, though I think this implementation is the simplest. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. This could indeed be a significant performance hit. That said, I'm OK leaving this for now, with a TODO note to optimize it later. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: you can drop the extra parentheses here inside
if