Skip to content
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

BUG: overrides to a dimension coordinate do not get aligned #3393

Merged
merged 1 commit into from
Oct 11, 2019
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
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