Skip to content

BUG: multi-index slicing buggy with datetime selectors #7429

Closed
@jreback

Description

@jreback

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']]

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesMultiIndex

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions