Skip to content

Commit 4c05d38

Browse files
shoyerdcherian
authored andcommitted
BUG: overrides to a dimension coordinate do not get aligned (#3393)
Fixes GH3377
1 parent 3f29551 commit 4c05d38

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

xarray/core/alignment.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,13 @@ def is_alignable(obj):
374374
elif is_dict_like(variables):
375375
current_out = OrderedDict()
376376
for k, v in variables.items():
377-
if is_alignable(v):
377+
if is_alignable(v) and k not in indexes:
378+
# Skip variables in indexes for alignment, because these
379+
# should to be overwritten instead:
380+
# https://github.com/pydata/xarray/issues/725
381+
# https://github.com/pydata/xarray/issues/3377
382+
# TODO(shoyer): doing this here feels super-hacky -- can we
383+
# move it explicitly into merge instead?
378384
positions.append(position)
379385
keys.append(k)
380386
targets.append(v)

xarray/tests/test_dataset.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3047,6 +3047,21 @@ def test_setitem_auto_align(self):
30473047
expected = Dataset({"x": ("y", [4, 5, 6])}, {"y": range(3)})
30483048
assert_identical(ds, expected)
30493049

3050+
def test_setitem_dimension_override(self):
3051+
# regression test for GH-3377
3052+
ds = xr.Dataset({"x": [0, 1, 2]})
3053+
ds["x"] = ds["x"][:2]
3054+
expected = Dataset({"x": [0, 1]})
3055+
assert_identical(ds, expected)
3056+
3057+
ds = xr.Dataset({"x": [0, 1, 2]})
3058+
ds["x"] = np.array([0, 1])
3059+
assert_identical(ds, expected)
3060+
3061+
ds = xr.Dataset({"x": [0, 1, 2]})
3062+
ds.coords["x"] = [0, 1]
3063+
assert_identical(ds, expected)
3064+
30503065
def test_setitem_with_coords(self):
30513066
# Regression test for GH:2068
30523067
ds = create_test_data()

0 commit comments

Comments
 (0)