Skip to content

Propagate MultiIndex variables in broadcast #6477

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

Merged
merged 2 commits into from
Apr 13, 2022
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
4 changes: 2 additions & 2 deletions xarray/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,8 @@ def _get_broadcast_dims_map_common_coords(args, exclude):
for dim in arg.dims:
if dim not in common_coords and dim not in exclude:
dims_map[dim] = arg.sizes[dim]
if dim in arg.coords:
common_coords[dim] = arg.coords[dim].variable
if dim in arg._indexes:
common_coords.update(arg.xindexes.get_all_coords(dim))

return dims_map, common_coords

Expand Down
12 changes: 12 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2331,6 +2331,18 @@ def test_broadcast_misaligned(self):
assert_identical(expected_x2, x2)
assert_identical(expected_y2, y2)

def test_broadcast_multi_index(self):
# GH6430
ds = Dataset(
{"foo": (("x", "y", "z"), np.ones((3, 4, 2)))},
{"x": ["a", "b", "c"], "y": [1, 2, 3, 4]},
)
stacked = ds.stack(space=["x", "y"])
broadcasted, _ = broadcast(stacked, stacked.space)

assert broadcasted.xindexes["x"] is broadcasted.xindexes["space"]
assert broadcasted.xindexes["y"] is broadcasted.xindexes["space"]

def test_variable_indexing(self):
data = create_test_data()
v = data["var1"]
Expand Down