Skip to content

Bug in broadcasting with multi-indexes #6430

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

Closed
dcherian opened this issue Mar 31, 2022 · 1 comment · Fixed by #6477
Closed

Bug in broadcasting with multi-indexes #6430

dcherian opened this issue Mar 31, 2022 · 1 comment · Fixed by #6477

Comments

@dcherian
Copy link
Contributor

What happened?

import numpy as np
import xarray as xr

ds = xr.Dataset(
    {"foo": (("x", "y", "z"), np.ones((3, 4, 2)))},
    {"x": ["a", "b", "c"], "y": [1, 2, 3, 4]},
)
expected = ds.sum("z")
stacked = ds.stack(space=["x", "y"])

broadcasted, _ = xr.broadcast(stacked, stacked.space)

stacked.sum("z").unstack("space")  # works
broadcasted.sum("z").unstack("space")  # error
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [13], in <module>
     10 broadcasted, _ = xr.broadcast(stacked, stacked.space)
     11 stacked.sum("z").unstack("space")
---> 12 broadcasted.sum("z").unstack("space")

File ~/work/python/xarray/xarray/core/dataset.py:4332, in Dataset.unstack(self, dim, fill_value, sparse)
   4330     non_multi_dims = set(dims) - set(stacked_indexes)
   4331     if non_multi_dims:
-> 4332         raise ValueError(
   4333             "cannot unstack dimensions that do not "
   4334             f"have exactly one multi-index: {tuple(non_multi_dims)}"
   4335         )
   4337 result = self.copy(deep=False)
   4339 # we want to avoid allocating an object-dtype ndarray for a MultiIndex,
   4340 # so we can't just access self.variables[v].data for every variable.
   4341 # We only check the non-index variables.
   4342 # https://github.com/pydata/xarray/issues/5902

ValueError: cannot unstack dimensions that do not have exactly one multi-index: ('space',)

What did you expect to happen?

This should work.

Minimal Complete Verifiable Example

No response

Relevant log output

No response

Anything else we need to know?

No response

Environment

xarray main after the flexible indexes refactor

@benbovy
Copy link
Member

benbovy commented Apr 2, 2022

Probably because we still need some internal refactoring in xr.broadcast (added in #6293).

broadcasted.xindexes
# Indexes:
# space: <xarray.core.indexes.PandasMultiIndex object at 0x163e5c6d0>
# y: <xarray.core.indexes.PandasMultiIndex object at 0x163e5c510>
# x: <xarray.core.indexes.PandasMultiIndex object at 0x163e5c510>

space and x / y should all have identical PandasMultiIndex objects but they don't.

dcherian added a commit to dcherian/xarray that referenced this issue Apr 12, 2022
dcherian added a commit to dcherian/xarray that referenced this issue Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants