I'm trying to slice a pandas dataframe indexed by a period index with a list of strings with unexpected results.
import pandas as pd
import numpy as np
idx = pd.period_range(1991,1993,freq='A')
df = pd.DataFrame(np.arange(9).reshape(3,3),index=idx)
print df.loc[['1991','1993'],:]
results in:
KeyError: "None of [['1991', '1993']] are in the [index]"
If the the last line is switched to:
print df.ix[['1991','1993'],:]
The output is
Out[128]:
0 1 2
1991 NaN NaN NaN
1993 NaN NaN NaN
If instead of a period index I have
idx = [str(year) for year in range(1991,1994)]
print df.loc[['1991','1993'],:]
Then the output is as expected:
Out[127]:
0 1 2
1991 0 1 2
1993 6 7 8
So my question is: how to slice a pandas dataframe with a period index?