BUG: multi-index slicing buggy with datetime selectors · Issue #7429 · pandas-dev/pandas (original) (raw)
http://stackoverflow.com/questions/24152509/slicing-a-pandas-multiindex-using-datetime-datatype
dates = pd.DatetimeIndex([datetime.datetime(2012,1,1,12,12,12)+datetime.timedelta(days = i) for i in range(6)])
freq = [1,2]
iterables = [dates, freq]
index = pd.MultiIndex.from_product(iterables, names=['date','frequency'])
df = pd.DataFrame(np.random.randn(6*2,4),index=index,columns=list('ABCD'))
- This should work in a single step
- show this using
pd.IndexSlice
notation - accept strings / partial strings as indexers
df_temp = df.loc[(slice(pd.Timestamp('2012-01-01 12:12:12'),pd.Timestamp('2012-01-03 12:12:12'))), slice('A','B')]
df_temp.loc[(slice(None),slice(1,1)),:]
After #7430, the following works (just using IndexSlice
as a conven)
idx = pd.IndexSlice
df.loc[(idx[pd.Timestamp('2012-01-01 12:12:12'),pd.Timestamp('2012-01-03 12:12:12'),idx[1:1]], idx['A','B']]
as well as partial string slicing
df.loc[idx['2012-01-01 12:12:12':'2012-01-03 12:12:12',1],idx['A':'B']]