You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.sel does not keep selected coordinate value in case with MultiIndex #1408
where MultiIndex structure is totally gone.
(ds.isel(yx=[0, 1]) works perfectly.)
I am now looking into the code. Do you guys have any idea to fix them?
I am thinking to modify Index.remap_label_indexers
so that it also returns selected_dims containing names of dims that should be converted to scalar coordinates.
The current behavior is actually dictated by pandas and I think it was initially intentional (before we could access MultiIndex levels as virtual coordinates).
In the first example MultiIndex.get_loc_level() is called, which doesn't return a MutliIndex:
In [11]: mdix=ds.yx.to_index()
In [12]: midx.get_loc_level(2, level='x')
Out[12]:
(array([False, True, False, False, True, False], dtype=bool),
Index(['a', 'b'], dtype='object', name='y'))
If we want to keep the MultiIndex in this case, then I think we can just ignore new_indexes returned by indexing.remap_label_indexers and remove Dataset._replace_indexes.
But we still have to choose whether automatically renaming the dimension makes sense or not, i.e., choose between
As a related issue, we could use an option for .sel to keep the selected variable as a singleton coordinate (an array of shape (1,)) instead of making it a scalar.
Activity
shoyer commentedon May 13, 2017
No, I don't think this was intentional/desired, though it might be slightly tricky to fix this with the current code structure.
@benbovy any thoughts?
For reference:
fujiisoup commentedon May 14, 2017
@shoyer Thanks for the information.
(Sorry for my inaccurate ipython output)
Another related issue is
where MultiIndex structure is totally gone.
(
ds.isel(yx=[0, 1])works perfectly.)I am now looking into the code. Do you guys have any idea to fix them?
I am thinking to modify Index.remap_label_indexers
so that it also returns
selected_dimscontaining names of dims that should be converted to scalar coordinates.benbovy commentedon May 15, 2017
The current behavior is actually dictated by pandas and I think it was initially intentional (before we could access MultiIndex levels as virtual coordinates).
In the first example
MultiIndex.get_loc_level()is called, which doesn't return aMutliIndex:If we want to keep the
MultiIndexin this case, then I think we can just ignorenew_indexesreturned byindexing.remap_label_indexersand remove Dataset._replace_indexes.But we still have to choose whether automatically renaming the dimension makes sense or not, i.e., choose between
and
The second issue is also due to how MultiIndex works in pandas:
I'm just wondering if it really makes sense to keep a MultiIndex for a coordinate that is not a dimension anymore like in this case.
fujiisoup commentedon May 15, 2017
@benbovy Thanks for the comment.
Actually, I expected the following behavior
where the selected
xis automatically converted to a scalar coordinate.And also for the second issue, I expected something like
both
xandybecome scalar coordinates.Back to the first issue, I personally prefer to the automatic renaming
because it enables the following chain methods,
that might be intuitive especially for the hierarchic dimension structure.
benbovy commentedon May 15, 2017
(mmh apparently I forgot to post a 2nd comment I wrote)
And this is just a nonsense :) Sorry it is late!
Yep I agree it would be nice to have scalar coordinates in both cases.
stale commentedon Sep 19, 2020
In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity
If this issue remains relevant, please comment here or remove the
stalelabel; otherwise it will be marked as closed automaticallygabriel-abrahao commentedon Nov 18, 2020
As a related issue, we could use an option for .sel to keep the selected variable as a singleton coordinate (an array of shape (1,)) instead of making it a scalar.
.sel() with multi-index: return scalar coords
benbovy commentedon Sep 15, 2021
@fujiisoup the behavior that you expect has been implemented in #5692 (I still need to fix/update the tests):
Note that for the latter example, the
yxcoordinate may eventually become irrelevant for multi-indexes (we'll need only the level coordinates).@gabriel-abrahao feel free to open a new issue for your suggestion.
Explicit indexes (#5692)