Skip to content

multi-index panel to xrarray dataarray index selection #850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
toniatop opened this issue May 19, 2016 · 4 comments
Closed

multi-index panel to xrarray dataarray index selection #850

toniatop opened this issue May 19, 2016 · 4 comments

Comments

@toniatop
Copy link

I am playing with xarray (moving away from pandas panel) but I cannot get my head around the following example. I create a multiindex panel and then convert it to a dataarray. I would like then to slice as I used to do in pandas , but trying the equivalent it fails with TypeError: '(slice(None, None, None), [3])' is an invalid key

Similarly I cannot get x.sel(major_axis=?) to work trying to achieve the same result.

import numpy as np
import pandas as pd
import xarray as xr
columns=['A','B','C']
index=pd.MultiIndex.from_product([['a','b','c','d'],[1,2,3]])
items=['x','y','z']
z=pd.Panel.from_dict({i:pd.DataFrame(data=np.random.rand(12,3),index=index,columns=columns) for i in items})
z.loc[:,(slice(None),[3]),'A'] #<-- That's what I want to do in xarray

x=z.to_xarray()
x.loc[:,(slice(None),[3]),'A'] #<- It fails

Do you have any insight?

@shoyer
Copy link
Member

shoyer commented May 20, 2016

Indeed, I'm afraid this currently doesn't work in xarray -- we need to use the get_loc_level method.

I believe @benbovy's fix in #802 should solve this (are you still working on that patch, Benoit?).

@benbovy
Copy link
Member

benbovy commented May 26, 2016

Sorry for the delay. I put #802 aside for some time but now I'm working on it again and hopefully I'll commit tests and docs in a couple of days.

#802 fixes the case above, although it currently requires to pass a dictionary with explicit index level names or numbers as keys:

x.loc[:, {0: slice(None), 1: 3}, 'A']  #<- It works with #802

@benbovy
Copy link
Member

benbovy commented May 30, 2016

After some additional commits in #802:

In [1]: x.loc[:, (slice(None), [3]), 'A']
Out[1]:
<xarray.DataArray (items: 3, major_axis: 4)>
array([[ 0.23589318,  0.28298667,  0.84166271,  0.22935685],
       [ 0.22939828,  0.47265628,  0.89810054,  0.17696009],
       [ 0.65109315,  0.7532165 ,  0.12492445,  0.12908149]])
Coordinates:
  * items       (items) object 'x' 'y' 'z'
  * major_axis  (major_axis) object ('a', 3) ('b', 3) ('c', 3) ('d', 3)
    minor_axis  <U1 'A'

@shoyer
Copy link
Member

shoyer commented Aug 11, 2016

Thanks to #802, this works now in xarray v0.8.

@shoyer shoyer closed this as completed Aug 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants