Skip to content

Commit

Permalink
BUG: overrides to a dimension coordinate do not get aligned (#3393)
Browse files Browse the repository at this point in the history
Fixes GH3377
  • Loading branch information
shoyer authored and dcherian committed Oct 11, 2019
1 parent 3f29551 commit 4c05d38
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion xarray/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,13 @@ def is_alignable(obj):
elif is_dict_like(variables):
current_out = OrderedDict()
for k, v in variables.items():
if is_alignable(v):
if is_alignable(v) and k not in indexes:
# Skip variables in indexes for alignment, because these
# should to be overwritten instead:
# https://github.com/pydata/xarray/issues/725
# https://github.com/pydata/xarray/issues/3377
# TODO(shoyer): doing this here feels super-hacky -- can we
# move it explicitly into merge instead?
positions.append(position)
keys.append(k)
targets.append(v)
Expand Down
15 changes: 15 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3047,6 +3047,21 @@ def test_setitem_auto_align(self):
expected = Dataset({"x": ("y", [4, 5, 6])}, {"y": range(3)})
assert_identical(ds, expected)

def test_setitem_dimension_override(self):
# regression test for GH-3377
ds = xr.Dataset({"x": [0, 1, 2]})
ds["x"] = ds["x"][:2]
expected = Dataset({"x": [0, 1]})
assert_identical(ds, expected)

ds = xr.Dataset({"x": [0, 1, 2]})
ds["x"] = np.array([0, 1])
assert_identical(ds, expected)

ds = xr.Dataset({"x": [0, 1, 2]})
ds.coords["x"] = [0, 1]
assert_identical(ds, expected)

def test_setitem_with_coords(self):
# Regression test for GH:2068
ds = create_test_data()
Expand Down

0 comments on commit 4c05d38

Please sign in to comment.