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

stack renames single dimensions instead of creating a MultiIndex #2802

Closed
equaeghe opened this issue Mar 5, 2019 · 3 comments
Closed

stack renames single dimensions instead of creating a MultiIndex #2802

equaeghe opened this issue Mar 5, 2019 · 3 comments

Comments

@equaeghe
Copy link

equaeghe commented Mar 5, 2019

Code Sample

>>> da = xr.DataArray([0., 1., 2.])
>>> db = da.stack(z=da.dims)
>>> db
<xarray.DataArray (z: 3)>
array([0., 1., 2.])
Coordinates:
  * z        (z) int64 0 1 2
>>> db.unstack('z')
Traceback (most recent call last):

  File "<ipython-input-127-ec66ebede9c5>", line 1, in <module>
    db.unstack('z')

  File "/usr/lib64/python3.6/site-packages/xarray/core/dataarray.py", line 1248, in unstack
    ds = self._to_temp_dataset().unstack(dim)

  File "/usr/lib64/python3.6/site-packages/xarray/core/dataset.py", line 2315, in unstack
    raise ValueError('cannot unstack a dimension that does not have '

ValueError: cannot unstack a dimension that does not have a MultiIndex

Problem description

stack is not consistently creating a MultiIndex, but just renames a dimension in case there is just the single dimension to be stacked. This breaks code where the (number of) dimensions are (is) not known in advance.

Expected Output

stack should create a MultiIndex in all cases and a stack/unstack sequence should always return the original DataArray.

Output of xr.show_versions()

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.19.23-gentoo
machine: x86_64
processor: Intel(R) Core(TM) i7-2620M CPU @ 2.70GHz
byteorder: little
LC_ALL: None
LANG: nl_BE.UTF-8
LOCALE: nl_BE.UTF-8

xarray: 0.10.8
pandas: 0.19.1
numpy: 1.14.5
scipy: 0.19.1
netCDF4: 1.3.1
h5netcdf: None
h5py: 2.9.0
Nio: None
zarr: None
bottleneck: 1.2.1
cyordereddict: None
dask: None
distributed: None
matplotlib: 2.2.2
cartopy: None
seaborn: None
setuptools: 40.6.3
pip: 9.0.1
conda: None
pytest: 3.10.1
IPython: 5.4.1
sphinx: 1.7.5

@max-sixty
Copy link
Collaborator

Thanks for the report @equaeghe

Is this a dupe of #1431 ?

@equaeghe
Copy link
Author

equaeghe commented Mar 5, 2019

Is this a dupe of #1431 ?

Yes. My example and explanation are a bit clearer, so I do not know which would be best to close.

In any case, I hope this is considered for fixing separately from some big refactoring of the MultiIndex machinery, which seems to have stalled. This seems like a corner case that might not be too big, but I may be mistaken.

@keewis
Copy link
Collaborator

keewis commented Jan 19, 2021

I think this has already been fixed:

>>> da.stack(z=da.dims)
<xarray.DataArray (z: 3)>
array([0., 1., 2.])
Coordinates:
  * z        (z) MultiIndex
  - dim_0    (z) int64 0 1 2

If you disagree, feel free to reopen.

@keewis keewis closed this as completed Jan 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants