.sel does not keep selected coordinate value in case with MultiIndex
See original GitHub issue.sel method usually keeps selected coordinate value as a scalar coordinate
In[4] ds1 = xr.Dataset({'foo': (('x',), [1, 2, 3])}, {'x': [1, 2, 3], 'y': 'a'})
Out[4]:
<xarray.Dataset>
Dimensions: ()
Coordinates:
y <U1 'a'
x int64 1
Data variables:
foo int64 1
But in MultiIndex case, does not.
In[5] ds2 = xr.Dataset({'foo': (('x',), [4, 5, 6])}, {'x': [1, 2, 3], 'y': 'b'})
ds = xr.concat([ds1, ds2], dim='y').stack(yx=['y', 'x'])
ds.sel(x=2)
Out[5]
<xarray.Dataset>
Dimensions: (y: 2)
Coordinates:
* y (y) object 'a' 'b'
Data variables:
foo (y) int64 2 5
x is gone. Is it a desired behavior?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:8 (7 by maintainers)
Top Results From Across the Web
Vectorized selection along level variable - Stack Overflow
do not exist . Another error message I see in trying to do this is ValueError: Vectorized selection is not available along level...
Read more >xarray.Dataset.sel
If indexer DataArrays have coordinates that do not conflict with this object, then these coordinates will be attached. In general, each array's data...
Read more >Indexing and selecting data — pandas 1.5.2 documentation
pandas aligns all AXES when setting Series and DataFrame from .loc , and .iloc . This will not modify df because the column...
Read more >Pandas MultiIndex Tutorial and Best Practices - YouTube
While Pandas is one of the most well known Python libraries for working with array-like data, many users limit themselves to just two ......
Read more >Data Structures - xarray - Read the Docs
This is no longer the case. Coordinates can be specified in the following ways: A list of values with length equal to the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@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.
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.