Skip to content
forked from pydata/xarray

Commit 76e7747

Browse files
committed
Propagate MultiIndex variables in broadcast
Fixes pydata#6430 xref pydata#6293
1 parent c12cb3d commit 76e7747

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

xarray/core/alignment.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,8 @@ def _get_broadcast_dims_map_common_coords(args, exclude):
927927
for dim in arg.dims:
928928
if dim not in common_coords and dim not in exclude:
929929
dims_map[dim] = arg.sizes[dim]
930-
if dim in arg.coords:
931-
common_coords[dim] = arg.coords[dim].variable
930+
if dim in arg.xindexes:
931+
common_coords.update(arg.xindexes[dim].create_variables())
932932

933933
return dims_map, common_coords
934934

xarray/tests/test_dataset.py

+12
Original file line numberDiff line numberDiff line change
@@ -2331,6 +2331,18 @@ def test_broadcast_misaligned(self):
23312331
assert_identical(expected_x2, x2)
23322332
assert_identical(expected_y2, y2)
23332333

2334+
def test_broadcast_multi_index(self):
2335+
# GH6430
2336+
ds = Dataset(
2337+
{"foo": (("x", "y", "z"), np.ones((3, 4, 2)))},
2338+
{"x": ["a", "b", "c"], "y": [1, 2, 3, 4]},
2339+
)
2340+
stacked = ds.stack(space=["x", "y"])
2341+
broadcasted, _ = broadcast(stacked, stacked.space)
2342+
2343+
assert broadcasted.xindexes["x"] is broadcasted.xindexes["space"]
2344+
assert broadcasted.xindexes["y"] is broadcasted.xindexes["space"]
2345+
23342346
def test_variable_indexing(self):
23352347
data = create_test_data()
23362348
v = data["var1"]

0 commit comments

Comments
 (0)