Skip to content

Label slices on Series with MultiIndex #8539

@LeartS

Description

@LeartS

I have a Series with a MultiIndex like this:

In [174]: a
Out[174]: 
hour  year  weekday  workingday       
0     2011  0        0           count      4.000000
                                 mean      76.750000
                                 std       53.368998
                                 min       17.000000
                                 25%       44.000000
                                 50%       75.000000
                                 75%      107.750000
                                 max      140.000000
                     1           count     28.000000
                                 mean      29.500000
                                 std       31.792732
                                 min        4.000000
                                 25%       15.000000
                                 50%       24.500000
                                 75%       35.500000
...
23    2012  5        0           mean     152.687500
                                 std       55.711490
                                 min       20.000000
                                 25%      119.000000
                                 50%      148.000000
                                 75%      187.750000
                                 max      239.000000
            6        0           count     34.000000
                                 mean      71.617647
                                 std       32.471913
                                 min       23.000000
                                 25%       46.000000
                                 50%       68.500000
                                 75%      104.250000
                                 max      123.000000
Length: 3456, dtype: float64

And I'd like to get, for example, the rows where hour=1, year=2011, weekday=[1,2,3,4]

Intuitively, I would do this: a.loc[1,2011,1:4] which would work in a 4-dimensional numpy array. Unfortunately it raises the error:

/usr/lib/python2.7/dist-packages/pandas/core/index.pyc in get_loc_level(self, key, level, drop_level)
   3214                             continue
   3215                         else:
-> 3216                             raise TypeError(key)
   3217 
   3218                     if indexer is None:

TypeError: (0, 2011, slice(0, 4, None))

Even If I try suppling the index list instead of a slice: a.loc[0,2011,[1,2,3,4]] I get an error: unhashable type: 'list'
Is there a reason why this syntax is not supported? How can I select labels slices in a MultiIndex?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions