question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.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:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
benbovycommented, Sep 15, 2021

@fujiisoup the behavior that you expect has been implemented in #5692 (I still need to fix/update the tests):

>>> ds.sel(x=2)
<xarray.Dataset>
Dimensions:  (y: 2)
Coordinates:
  * y        (y) <U1 'a' 'b'
    x        int64 2
Data variables:
    foo      (y) int64 2 5
>>> ds.isel(yx=0)
<xarray.Dataset>
Dimensions:  ()
Coordinates:
    yx       object ('a', 1)
    y        <U1 'a'
    x        int64 1
Data variables:
    foo      int64 1

Note that for the latter example, the yx coordinate 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.

0reactions
gabriel-abrahaocommented, 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found