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/7076#issuecomment-1431400086,https://api.github.com/repos/pydata/xarray/issues/7076,1431400086,IC_kwDOAMm_X85VUW6W,18679148,2023-02-15T13:49:45Z,2023-02-16T13:43:29Z,NONE,"Hi @DWesl, @benbovy I got a bug very similar to what was initially report with the following minimal example (xarray version 2022.11.0) with the error: > ValueError: cannot unstack dimensions that do not have exactly one multi-index: ('z',) It is working fine with my previous version (xarray 0.16.0) ```python import xarray as xr ds = xr.Dataset( data_vars=dict( mydata = ( ['across', 'along'], [[0,1],[2,3]]) ) ) stacked = ds.stack(z=(""across"", ""along"")) newlist = [None] * stacked.z.size for ii, zindex in enumerate(stacked.z.data): newlist[ii] = stacked.mydata.sel(z=zindex) newds = xr.concat(newlist, dim='z') newds['z'] = stacked.z print('xarray version:' + xr.__version__) newds.unstack(dim='z') ``` If I do `newds = newds.reset_index('z')` just before the last line, it is solved for my minimal example. But it still doesn't work for my real problem and I don't think it is the best way to proceed. Many thanks for your help, Adrien ","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,1384465119 https://github.com/pydata/xarray/issues/7076#issuecomment-1433104955,https://api.github.com/repos/pydata/xarray/issues/7076,1433104955,IC_kwDOAMm_X85Va3I7,18679148,2023-02-16T13:41:12Z,2023-02-16T13:41:12Z,NONE,"> Hi @benbovy, my code works for 2 dimensions, but it doesn't with 1D. I think the error comes from `.set_index(z=['across'])` which delivers a ""PandasIndex(Int64Index("" and not the expected ""PandasIndex(MultiIndex("" for `unstack`. I created a Pandas-MultiIndex ""MultiIndex([((0,), (1,))], names=['z', 'across'])"" (see code just below), but it is not exactly the same one as the one created by `.stack()` ""PandasIndex(MultiIndex([(0,),(1,)], name='z'))"" and I didn't find out how to pass the 'index' to xarray. ```python import pandas as pd tuples = [(0,),(1,)], index = pd.MultiIndex.from_tuples(tuples, names=[""z"",""across""]) index ``` My modified minimal example is as below: ```python import xarray as xr ds = xr.Dataset( data_vars=dict( mydata = ( ['across', 'along'], [[0,1],[2,3]]) ), coords=dict( # <- changed across=[0,1], # <- changed along=[0,1], # <- changed ), # <- changed ) stacked = ds.isel(along=0).stack(z=[""across""]) # <- changed newlist = [None] * stacked.z.size for ii, zindex in enumerate(stacked.z.data): newlist[ii] = stacked.mydata.sel(z=zindex) newds = xr.concat(newlist, dim='z') newds = newds.set_index(z=['across']) # <- changed newds.unstack(dim='z') # Fail with ValueError: cannot unstack dimensions that do not have exactly one multi-index: ('z',) ``` ","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,1384465119 https://github.com/pydata/xarray/issues/7076#issuecomment-1431736710,https://api.github.com/repos/pydata/xarray/issues/7076,1431736710,IC_kwDOAMm_X85VVpGG,18679148,2023-02-15T17:24:27Z,2023-02-15T17:24:27Z,NONE,"> Many thanks for the explanation and the rapid answer. I am not very familiar with multi-index. > > My real problem works with a DataSet with different sort of variables: > > ``` > ipdb> lmmap > > Dimensions: (x_variables: 4, Observables: 2, Antenna: 4, z: 4, > Ambiguities: 4, fun_variables: 8, extrema: 2) > Coordinates: > * x_variables (x_variables) * Observables (Observables) * Antenna (Antenna) fun_variables (Observables, Antenna) int64 0 1 2 3 4 5 6 7 > Dimensions without coordinates: z, Ambiguities, extrema > Data variables: (12/17) > x (z, Ambiguities, x_variables) float64 -4.503 7.8 ... -0.4265 > active_mask (z, Ambiguities, x_variables) int64 0 0 0 0 0 0 ... 0 0 0 0 0 > grad (z, Ambiguities, x_variables) float64 7.816e-11 ... -1.873... > fun (z, Ambiguities, Observables, Antenna) float64 0.001137 ..... > jac (z, Ambiguities, fun_variables, x_variables) float64 0.359... > x0 (z, Ambiguities, x_variables) float64 -7.002 10.03 ... 1.219 > ... ... > message (z, Ambiguities) success (z, Ambiguities) bool True True True True ... True True True > method (z) xtol (z) float64 0.001 0.001 0.001 0.001 > x_scale (z, x_variables) float64 7.0 7.0 0.5 0.5 ... 7.0 7.0 0.5 0.5 > bounds (z, extrema, x_variables) int64 -30 -30 -5 -5 ... 30 30 5 5 > ``` > > I don't know where I should apply `set_index(z=[""across"", ""along""])` I tried on the full DataSet or a single DataArray but without success. I get the following error: > > > ValueError: across, along variable(s) do not exist > > Which is obvious as across and along variables disappeared. Thank you, I will dig in this direction why it happens. > > However, it works perfectly with the minimal example. Many thanks Thanks @benbovy, my issue is now resolved, once I have enabled the coords to follow.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,1384465119 https://github.com/pydata/xarray/issues/7076#issuecomment-1431676036,https://api.github.com/repos/pydata/xarray/issues/7076,1431676036,IC_kwDOAMm_X85VVaSE,18679148,2023-02-15T16:51:49Z,2023-02-15T16:51:49Z,NONE,I didn't see any warning for this,"{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,1384465119 https://github.com/pydata/xarray/issues/7076#issuecomment-1431645539,https://api.github.com/repos/pydata/xarray/issues/7076,1431645539,IC_kwDOAMm_X85VVS1j,2448579,2023-02-15T16:30:21Z,2023-02-15T16:30:21Z,MEMBER,"> the issue is when you do newds['z'] = stacked.z Are we raising a warning here?","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,1384465119 https://github.com/pydata/xarray/issues/7076#issuecomment-1431529928,https://api.github.com/repos/pydata/xarray/issues/7076,1431529928,IC_kwDOAMm_X85VU2nI,18679148,2023-02-15T15:15:23Z,2023-02-15T15:20:58Z,NONE,"Many thanks for the explanation and the rapid answer. I am not very familiar with multi-index. My real problem works with a DataSet with different sort of variables: ``` ipdb> lmmap Dimensions: (x_variables: 4, Observables: 2, Antenna: 4, z: 4, Ambiguities: 4, fun_variables: 8, extrema: 2) Coordinates: * x_variables (x_variables) ValueError: across, along variable(s) do not exist Which is obvious as across and along variables disappeared. Thank you, I will dig in this direction why it happens. However, it works perfectly with the minimal example. Many thanks ","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,1384465119 https://github.com/pydata/xarray/issues/7076#issuecomment-1431496828,https://api.github.com/repos/pydata/xarray/issues/7076,1431496828,IC_kwDOAMm_X85VUuh8,4160723,2023-02-15T14:54:27Z,2023-02-15T14:54:27Z,MEMBER,"@ACHMartin the issue is when you do `newds['z'] = stacked.z`. In the last versions of Xarray multi-index levels have each their own (real) coordinates, for consistency and clarity we soon won't support assigning a multi-index to a single coordinate of a Dataset / DataArray like that. I think that in other places we still do support it with a deprecation notice, but apparently in your example this is not the case. `unstack` doesn't work because the multi-index(es) and the coordinates of `newds` are not consistent. I don't know exactly what is your real problem, but from now on you should avoid implicitly assign a multi-index with `xr_obj[""my_coord""] = ...` or `xr_obj.assign(my_coord=...)`. Instead you should re-create the multi-index, e.g., in your minimal example `newds = newds.set_index(z=[""across"", ""along""])`.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,1384465119 https://github.com/pydata/xarray/issues/7076#issuecomment-1259894192,https://api.github.com/repos/pydata/xarray/issues/7076,1259894192,IC_kwDOAMm_X85LGHWw,22566757,2022-09-27T18:28:26Z,2022-09-27T18:28:26Z,CONTRIBUTOR,"Fix confirmed, thank you.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,1384465119 https://github.com/pydata/xarray/issues/7076#issuecomment-1257965977,https://api.github.com/repos/pydata/xarray/issues/7076,1257965977,IC_kwDOAMm_X85K-wmZ,4160723,2022-09-26T12:32:59Z,2022-09-26T12:32:59Z,MEMBER,"Hi @DWesl, Your example is working with the `main` branch, it has been fixed in #6889. ```python # after #6889 arr2.unstack(""index0"") # # array([[[ 0, 1, 2, 3, 4], # [ 5, 6, 7, 8, 9], # [10, 11, 12, 13, 14]], # # [[ 0, 1, 2, 3, 4], # [ 5, 6, 7, 8, 9], # [10, 11, 12, 13, 14]]]) # Coordinates: # * level_0 (level_0) int64 0 1 2 # * level_1 (level_1) int64 0 1 2 3 4 # Dimensions without coordinates: index2 ```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,1384465119