html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue
https://github.com/pydata/xarray/issues/2933#issuecomment-1094066462,https://api.github.com/repos/pydata/xarray/issues/2933,1094066462,IC_kwDOAMm_X85BNiEe,5635139,2022-04-09T15:23:28Z,2022-04-09T15:23:28Z,MEMBER,"Thanks to @0x0L for the answers. I'll close the issue now, please reopen if there are outstanding questions","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,438947247
https://github.com/pydata/xarray/issues/2933#issuecomment-709313477,https://api.github.com/repos/pydata/xarray/issues/2933,709313477,MDEyOklzc3VlQ29tbWVudDcwOTMxMzQ3Nw==,3621629,2020-10-15T13:09:18Z,2020-10-15T13:09:18Z,CONTRIBUTOR,"You could do it by creating a flatten view of the underlying numpy array, building the multiindex manually and creating a new DataArray from the view and the multiindex.","{""total_count"": 1, ""+1"": 1, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,438947247
https://github.com/pydata/xarray/issues/2933#issuecomment-709289668,https://api.github.com/repos/pydata/xarray/issues/2933,709289668,MDEyOklzc3VlQ29tbWVudDcwOTI4OTY2OA==,1200058,2020-10-15T12:37:10Z,2020-10-15T12:37:10Z,NONE,"Is there a way without unstacking as well?
(Unstack can be quite wasteful)","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,438947247
https://github.com/pydata/xarray/issues/2933#issuecomment-499248139,https://api.github.com/repos/pydata/xarray/issues/2933,499248139,MDEyOklzc3VlQ29tbWVudDQ5OTI0ODEzOQ==,3621629,2019-06-05T20:44:29Z,2019-06-05T20:44:29Z,CONTRIBUTOR,"Hello @ray306
stack/unstack in xarray is a bit different from pandas mechanics.
- From the `stack` docstring
```
Stack any number of existing dimensions into a single new dimension.
New dimensions will be added at the end, and the corresponding
coordinate variables will be combined into a MultiIndex.
```
Breaking it down, `da.stack({'index': ['variable']})` fails because it's trying to:
1. create a new multi-index with a single level inside (`variable`) [so far so good but probably not what you had in mind]
2. add a new dimension named 'index' with coordinates given by the newly created index. This part yields the error you saw since `da` already has an index named `index`.
To mesh the `variable` dimension into the `index` dimension, you first need to unstack it:
```python
da.unstack('index').stack(index=('first', 'second', 'variable'))
```
- From the `unstack` docstring
```
Unstack existing dimensions corresponding to MultiIndexes into
multiple new dimensions.
Parameters
----------
dim : str or sequence of str, optional
Dimension(s) over which to unstack. By default unstacks all
MultiIndexes.
```
Your `da` array only has two dimensions: `variable` and `index`. That's why you can't `da.unstack('first')`. `first` is a coordinate (along the dimension 'index') not a dimension.
You could use
```
da.unstack('index').stack(index=['second'])
```
but this is somehow the same thing as `da.unstack()` since a multi-index with a single level is totally equivalent to the level itself.
I hope this helps you enjoy xarray and use it more.","{""total_count"": 1, ""+1"": 1, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,438947247