id,node_id,number,title,user,state,locked,assignee,milestone,comments,created_at,updated_at,closed_at,author_association,active_lock_reason,draft,pull_request,body,reactions,performed_via_github_app,state_reason,repo,type 438947247,MDU6SXNzdWU0Mzg5NDcyNDc=,2933,Stack() & unstack() issues on Multindex,1559890,closed,0,,,4,2019-04-30T19:47:51Z,2022-04-09T15:23:28Z,2022-04-09T15:23:28Z,NONE,,,,"I would like to reshape the DataArray by one level in the Multindex, and I thought the `stack()`/`unstack()` should be the solution. Make a DataArray with Multindex: ```python import pandas as pd arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo']), np.array(['one', 'two', 'one', 'two', 'one', 'two'])] da = pd.DataFrame(np.random.randn(6, 4)).to_xarray().to_array() da.coords['index'] = pd.MultiIndex.from_arrays(arrays, names=['first', 'second']) da ``` ``` array([[ 0.379189, 1.082292, -2.073478, -0.84626 , -1.529927, -0.837407], [-0.267983, -0.2516 , -1.016653, -0.085762, -0.058382, -0.667891], [-0.013488, -0.855332, -0.038072, -0.385211, -2.149742, -0.304361], [ 1.749561, -0.606031, 1.914146, 1.6292 , -0.515519, 1.996283]]) Coordinates: * index (index) MultiIndex - first (index) object 'bar' 'bar' 'baz' 'baz' 'foo' 'foo' - second (index) object 'one' 'two' 'one' 'two' 'one' 'two' * variable (variable) int32 0 1 2 3 ``` ### Stack problem: I want a dimension merges into another one: ```python da.stack({'index':['variable']}) ``` ``` ValueError: cannot create a new dimension with the same name as an existing dimension ``` ### Unstack problem: Unstacking by the whole Multindex worked: ```python da.unstack('index') ``` ``` array([[[ 0.379189, 1.082292], [-2.073478, -0.84626 ], [-1.529927, -0.837407]], [[-0.267983, -0.2516 ], [-1.016653, -0.085762], [-0.058382, -0.667891]], [[-0.013488, -0.855332], [-0.038072, -0.385211], [-2.149742, -0.304361]], [[ 1.749561, -0.606031], [ 1.914146, 1.6292 ], [-0.515519, 1.996283]]]) Coordinates: * variable (variable) int32 0 1 2 3 * first (first) object 'bar' 'baz' 'foo' * second (second) object 'one' 'two' ``` But unstacking by a specified level failed: ```python da.unstack('first') ``` ``` ValueError: Dataset does not contain the dimensions: ['first'] ```","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/2933/reactions"", ""total_count"": 1, ""+1"": 1, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,completed,13221727,issue 602793814,MDU6SXNzdWU2MDI3OTM4MTQ=,3984,Support flexible DataArray shapes in Dataset ,1559890,closed,0,,,4,2020-04-19T18:57:07Z,2020-09-26T00:02:12Z,2020-09-26T00:02:12Z,NONE,,,,"I always use Pandas to deal with my neuroscience data (multi-dimension). It is annoying to stack and unstack all the time and I heard Xarray is designed for multi-dimension data. In neuroscience research, we usually have multiple participants and we will test them different times, which means the data may look like this: - participant A: - 2\*5\*100 matrix - participant B: - 2\*5\*101 matrix (100 and 101 are the testing times) But **Dataset doesn't support to have 2\*5\*100 DataArray and 2\*5\*101 DataArray together**. Is there any solution to deal with that kind of data in Xarray?","{""url"": ""https://api.github.com/repos/pydata/xarray/issues/3984/reactions"", ""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,completed,13221727,issue