Skip to content

BUG: indexing with string in multi-index for Period gives KeyError #9892

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

From SO:

import pandas as pd
import itertools
a = pd.period_range('2013Q1','2013Q4', freq='Q')
i = (1111, 2222, 3333)
idx = pd.MultiIndex.from_tuples(list(itertools.product(a, i)),
                            names=('Periode', 'CVR'))
df = pd.DataFrame(index=idx,
              columns=('OMS', 'OMK','RES','DRIFT_IND','OEVRIG_IND','FIN_IND','VARE_UD','LOEN_UD','FIN_UD'))

Trying to select with a string gives a ValueError:

In [15]: df.loc[('2013Q1',1111),'OMS']
---------------------------------------------------------------------------
KeyError: ('2013Q1', 1111)

while it works with an explicit Period:

In [16]: df.loc[(pd.Period('2013Q1'),1111),'OMS']
Out[16]: nan

and it also works when not using multi-index notation (only accessing the first level): df.loc['2013Q1', 'OMS']

and this did work in 0.14.1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions