Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mgoodhand opened this issue Apr 13, 2018 · 4 comments
Closed

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

mgoodhand opened this issue Apr 13, 2018 · 4 comments
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Period Period data type
Milestone

Comments

@mgoodhand
Copy link

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

@jreback jreback added Indexing Related to indexing on series/frames, not to indexes themselves Period Period data type MultiIndex Difficulty Intermediate labels Apr 14, 2018
@jreback jreback added this to the Next Major Release milestone Apr 14, 2018
@jreback
Copy link
Contributor

jreback commented Apr 14, 2018

periods in a multiindex are not very well tested / supported for indexing. you are welcome to submit a patch.

@proost
Copy link
Contributor

proost commented Aug 16, 2019

May i take a look?

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Aug 16, 2019 via email

proost added a commit to proost/pandas that referenced this issue Sep 26, 2019
(pandas-dev#20684)

If index is MultiIndex and level of 0 is PeriodIndex, loc function raise
exception if all input of loc does not match index values
proost added a commit to proost/pandas that referenced this issue Sep 28, 2019
(pandas-dev#20684)

If index is MultiIndex and level of 0 is PeriodIndex, loc function raise
exception if all input of loc does not match index values
proost added a commit to proost/pandas that referenced this issue Oct 10, 2019
(pandas-dev#20684)

If `MultiIndex` nlevel is 3 and first level index is `PeriodIndex`,
`.loc` would raise exception with a miss match key
proost added a commit to proost/pandas that referenced this issue Oct 10, 2019
(pandas-dev#20684)

If `MultiIndex` nlevel is 3 and first level index is `PeriodIndex`,
`.loc` would raise exception with a miss match key
proost added a commit to proost/pandas that referenced this issue Oct 10, 2019
(pandas-dev#20684)

If `MultiIndex` nlevel is 3 and first level index is `PeriodIndex`,
`.loc` would raise exception with a miss match key
proost added a commit to proost/pandas that referenced this issue Oct 16, 2019
(pandas-dev#20684)

If `MultiIndex` is in PeriodIndex, `.loc` would raise exception with a miss match key
proost added a commit to proost/pandas that referenced this issue Oct 22, 2019
(pandas-dev#20684)

If `MultiIndex` is in PeriodIndex, `.loc` would raise exception with a miss match key

Remove `PeriodEngine.get_loc` in `PeriodIndex.get_loc`
proost added a commit to proost/pandas that referenced this issue Oct 23, 2019
(pandas-dev#20684)

If `MultiIndex` is in PeriodIndex, `.loc` would raise exception with a miss match key

`PeriodEngine.get_loc` can't accept Period
proost added a commit to proost/pandas that referenced this issue Oct 23, 2019
(pandas-dev#20684)

If `MultiIndex` is in PeriodIndex, `.loc` would raise exception with a miss match key

`PeriodEngine.get_loc` only accept Period.ordinal
proost added a commit to proost/pandas that referenced this issue Oct 23, 2019
(pandas-dev#20684)

If `MultiIndex` is in PeriodIndex, `.loc` would raise exception with a miss match key

`PeriodEngine.get_loc` only accept Period.ordinal
proost added a commit to proost/pandas that referenced this issue Oct 24, 2019
(pandas-dev#20684)

If `MultiIndex` is in PeriodIndex, `.loc` would raise exception with a miss match key

`PeriodEngine.get_loc` only accept Period.ordinal
proost added a commit to proost/pandas that referenced this issue Oct 24, 2019
(pandas-dev#20684)

If `MultiIndex` is in PeriodIndex, `.loc` would raise exception with a miss match key
proost added a commit to proost/pandas that referenced this issue Oct 24, 2019
(pandas-dev#20684)

If `MultiIndex` is in PeriodIndex, `.loc` would raise exception with a miss match key
proost added a commit to proost/pandas that referenced this issue Oct 25, 2019
(pandas-dev#20684)

If `MultiIndex` is in PeriodIndex, `.loc` would raise exception with a miss match key
@jreback jreback modified the milestones: Contributions Welcome, 1.0 Oct 25, 2019
@TomAugspurger
Copy link
Contributor

This was fixed by #28628

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Period Period data type
Projects
None yet
Development

No branches or pull requests

5 participants