Skip to content

loc misbehaves when Period is at start of 3-level MultiIndex #20684

Closed
@mgoodhand

Description

@mgoodhand

Code Sample

>>> pbc = pd.DataFrame({ 'p': [pd.Period('2017')], 'b':['b1'], 'c': ['c1'], 'v': [1]}).set_index(['p','b','c'])
>>> pbc.loc[(pd.Period('2017'), 'b1', 'c1'), 'v']
b   c
b1  c1    1
Name: v, dtype: int64
>>> pbc.loc[(pd.Period('2017'), 'wibble', 'c1'), 'v']
b   c
b1  c1    1
Name: v, dtype: int64
>>> pbc.loc[(pd.Period('2017'), 'b1', 'wobble'), 'v']
b   c
b1  c1    1
Name: v, dtype: int64

Problem description

I'd expect loc to return a scalar value when there's a unique match, rather than a Series with one item in it.

I'd also expect a KeyError when the second and third levels of the index don't match (the 'wibble' and 'wobble' selections). Instead it appears that the additional levels are ignored, and matches succeed or fail based on the period alone.

Expected Output

Everything works as I expect when I use strings instead of Periods:

>>> ybc = pd.DataFrame({ 'y': ['2017'], 'b':['b1'], 'c': ['c1'], 'v': [1]}).set_index(['y','b','c'])
>>> ybc.loc[('2017', 'b1', 'c1'), 'v']
1
>>> ybc.loc[('2017', 'b1', 'wobble'), 'v']
...
KeyError: ('2017', 'b1', 'wobble')

Things also work as I'd expect when there are only two levels in the MultiIndex (or four, or five):

>>> pb = pd.DataFrame({ 'p': [pd.Period('2017')], 'b':['b1'], 'v': [1]}).set_index(['p','b'])
>>> pb.loc[(pd.Period('2017'), 'b1'), 'v']
1
>>> pb.loc[(pd.Period('2017'), 'wibble'), 'v']
...
KeyError: (Period('2017', 'A-DEC'), 'wibble')

It also works if I put Period last in the three-level MultiIndex:

>>> bcp = pd.DataFrame({ 'p': [pd.Period('2017')], 'b':['b1'], 'c': ['c1'], 'v': [1]}).set_index(['b','c', 'p'])
>>> bcp.loc[('b1', 'c1', pd.Period('2017')), 'v']
1
>>> bcp.loc[('b1', 'wibble', pd.Period('2017')), 'v']
...
KeyError: ('b1', 'wibble', Period('2017', 'A-DEC'))

... or second:

>>> bpc = pd.DataFrame({ 'p': [pd.Period('2017')], 'b':['b1'], 'c': ['c1'], 'v': [1]}).set_index(['b','p','c'])
>>> bpc.loc[('b1', pd.Period('2017'), 'c1'), 'v']
1
>>> bpc.loc[('b1', pd.Period('2017'), 'wibble'), 'v']
...
KeyError: ('b1', Period('2017', 'A-DEC'), 'wibble')

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

INSTALLED VERSIONS

commit: None
python: 2.7.10.final.0
python-bits: 64
OS: Darwin
OS-release: 17.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: None.None

pandas: 0.22.0
pytest: None
pip: 9.0.3
setuptools: 39.0.1
Cython: None
numpy: 1.14.2
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: 0.5.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexingRelated to indexing on series/frames, not to indexes themselvesMultiIndexPeriodPeriod data type

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions