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/4417#issuecomment-1245036513,https://api.github.com/repos/pydata/xarray/issues/4417,1245036513,IC_kwDOAMm_X85KNb_h,3958036,2022-09-13T07:54:32Z,2022-09-13T07:54:32Z,CONTRIBUTOR,"Thanks @benbovy - with explicit methods now to produce the result with or without index, I think we can close this now :smiley: ","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,698577111 https://github.com/pydata/xarray/issues/4417#issuecomment-691109135,https://api.github.com/repos/pydata/xarray/issues/4417,691109135,MDEyOklzc3VlQ29tbWVudDY5MTEwOTEzNQ==,3958036,2020-09-11T13:54:01Z,2020-09-11T13:54:01Z,CONTRIBUTOR,"> I think I was trying to fix this in #4108 :+1: On a quick glance it looks to me like #4108 would fix my issue here.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,698577111 https://github.com/pydata/xarray/issues/4417#issuecomment-691045790,https://api.github.com/repos/pydata/xarray/issues/4417,691045790,MDEyOklzc3VlQ29tbWVudDY5MTA0NTc5MA==,3958036,2020-09-11T11:45:40Z,2020-09-11T11:45:40Z,CONTRIBUTOR,"I haven't managed to get `set_index` to line up the results. This: ``` import numpy as np import xarray as xr ds = xr.Dataset() ds['a'] = ('x', np.linspace(0,1)) ds['b'] = ('x', np.linspace(3,4)) ds = ds.rename(b='x') ds = ds.set_coords('x') ds = ds.set_index(x='x') print(ds) print('indexes', ds.indexes) ``` Produces a Dataset that now has an index `x`, but no `x` coordinate. The 'assignment of 1D variable' version produces both a coordinate and an index. One suggested solution in #2461 was to use `swap_dims()`, and the following does produce the desired result ``` import numpy as np import xarray as xr ds = xr.Dataset() ds['a'] = ('x', np.linspace(0,1)) ds['b'] = ('x', np.linspace(3,4)) ds = ds.swap_dims({'x': 'b'}) ds = ds.rename(b='x') print(ds) print('indexes', ds.indexes) ``` But (1) having to use `swap_dims()` to create a coordinate seems bizarre, (2) I think it's a bit ugly to get rid of the `x` dimension with `swap_dims()`, and then have to rename the new dimension back to `x`, when what I wanted was to add a coordinate to `x`. I found the behaviour confusing because I wasn't aware of the index variables at all... I create a coordinate with `set_coords()`; then do a bunch of manipulations involving slicing pieces out of the Dataset and re-combining them with `concat` (at least I think this is the relevant part of my code...); finally test the result by taking slices again (with `isel()`) of the result and comparing them to slices of the original Dataset with `xarray.testing.assert_identical()`, which failed because of the missing indexes. I guess somewhere in the manipulations I did, some operation created the coordinate in a new Dataset object by assignment, at which point it generated an index for the coordinate too. I may well be missing other context that makes this an undesirable thing to do, but for my use-case at least, I think it would make more sense if `set_coords()` created an index if the coordinate is a dimension coordinate (or whatever the actual criterion is for assignment of a 1d variable to create an index).","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,698577111