Question

I am trying do use a pandas multiindex to select a partial slice at the top level index (date), and apply a list to the second level index (stock symbol). I.e. below I want the data for AAPL and MSFT in the range d1:d2.

The partial slice works fine, however it is not clear how to select both AAPL and MSFT from the second index, while avoiding GOOG in the middle.

If I swap the levels it works with a single symbol, but not a list.

In [93]: print df
                 f1  f2  c1
date       sym
2012-01-01 AAPL  5.  2   3
           GOOG  1.  2   3
           MSFT  4.  2   3
2012-01-02 AAPL  8.  2   3
           GOOG  6.  2   3
           MSFT  7.  2   3
2012-01-03 AAPL  11  2   3
           GOOG  9.  2   3
           MSFT  10  2   3

In [94]: print df.ix[d1:d2].swaplevel(0,1).ix['AAPL']
            f1  f2  c1
date
2012-01-01  5   2   3
2012-01-02  8   2   3

In [95]: print df.ix[d1:d2].swaplevel(0,1).ix[['AAPL', 'MSFT']]
<blah balh>
TypeError: Expected tuple, got str

I want to AVOID building a long tuple list i.e.:

t = [(d1, 'AAPL'), (d1, 'MSFT'), (d2, 'AAPL'), (d2, 'MSFT')]

which DOES work when passed to ix. Below is my desired output.

In [103]: print df.ix[t]
                 f1  f2  c1
date       sym
2012-01-01 AAPL  5   2   3
           MSFT  4   2   3
2012-01-02 AAPL  8   2   3
           MSFT  7   2   3

Thanks, John

Was it helpful?

Solution

It works for me with pandas 0.7.2:

print df.ix[d1:d2].swaplevel(0,1).ix[['AAPL', 'MSFT']]
                 f1  f2  c1
sym  date                  
AAPL 2012-01-01   5   2   3
MSFT 2012-01-01   4   2   3
AAPL 2012-01-02   8   2   3
MSFT 2012-01-02   7   2   3

import pandas; pandas.__version__
'0.7.2'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top