Skip to content

BUG: loc with MultiIndex as index is returning incorrect Index #46704

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

Open
2 of 3 tasks
galipremsagar opened this issue Apr 8, 2022 · 6 comments
Open
2 of 3 tasks

BUG: loc with MultiIndex as index is returning incorrect Index #46704

galipremsagar opened this issue Apr 8, 2022 · 6 comments
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex

Comments

@galipremsagar
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

>>> import pandas as pd
>>> s = pd.DataFrame(
...         {"a": [1, 1, 2], "b": [1, 2, 3], "c": ["a", "b", "c"]}
...     ).set_index(["a", "b"])["c"]
>>> s.loc[(1, slice(None))]
b
1    a
2    b
Name: c, dtype: object
>>> print(pd.__version__)
1.4.2

Issue Description

It appears to be that one level is being dropped when loc is called.

Expected Behavior

import pandas as pd
s = pd.DataFrame(
        {"a": [1, 1, 2], "b": [1, 2, 3], "c": ["a", "b", "c"]}
    ).set_index(["a", "b"])["c"]

print(s.loc[(1, slice(None))])
a  b
1  1    a
   2    b
Name: c, dtype: object
print(pd.__version__)
1.3.5

Installed Versions

pd.show_versions()
Traceback (most recent call last):
File "", line 1, in
File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/util/_print_versions.py", line 109, in show_versions
deps = _get_dependency_info()
File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/util/_print_versions.py", line 88, in _get_dependency_info
mod = import_optional_dependency(modname, errors="ignore")
File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/pandas/compat/_optional.py", line 138, in import_optional_dependency
module = importlib.import_module(name)
File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1014, in _gcd_import
File "", line 991, in _find_and_load
File "", line 975, in _find_and_load_unlocked
File "", line 671, in _load_unlocked
File "", line 843, in exec_module
File "", line 219, in _call_with_frames_removed
File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/setuptools/init.py", line 8, in
import _distutils_hack.override # noqa: F401
File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/_distutils_hack/override.py", line 1, in
import('_distutils_hack').do_override()
File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/_distutils_hack/init.py", line 72, in do_override
ensure_local_distutils()
File "/nvme/0/pgali/envs/cudfdev/lib/python3.8/site-packages/_distutils_hack/init.py", line 59, in ensure_local_distutils
assert '_distutils' in core.file, core.file
AssertionError: /nvme/0/pgali/envs/cudfdev/lib/python3.8/distutils/core.py

@galipremsagar galipremsagar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 8, 2022
@galipremsagar galipremsagar changed the title BUG: loc when MultiIndex is returning incorrect Index BUG: loc with MultiIndex as index is returning incorrect Index Apr 8, 2022
@kwhkim
Copy link
Contributor

kwhkim commented Apr 9, 2022

I think this is not a bug.

compare this with singleIndex case, s.loc[1] which loses index and return a scalar

or compare with s.loc[(slice(1,1), slice(None))] which returns data frame with multi index

@rhshadrach
Copy link
Member

Agreed @kwhkim, this is documented in e.g. https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html.

Is there documentation that lead you to believe the level should not be dropped @galipremsagar?

@rhshadrach rhshadrach added Indexing Related to indexing on series/frames, not to indexes themselves Usage Question Needs Info Clarification about behavior needed to assess issue and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 9, 2022
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Apr 10, 2022
@simonjayhawkins
Copy link
Member

simonjayhawkins commented Apr 10, 2022

Thanks @galipremsagar for the report

It appears to be that one level is being dropped when loc is called.

This was a bug fixed in commit: [35b338e] BUG: .loc failing to drop first level (#42435) but unfortunately there was no issue opened related to this to allow discussion or even a release note to communicate the change in behavior.

Note: there does still seem to be some inconsistent behavior and #42435 mentioned that there are other issues.

>>> s.loc[1]
b
1    a
2    b
Name: c, dtype: object
>>> 
>>> s.loc[
...     1,
... ]
b
1    a
2    b
Name: c, dtype: object
>>> 
>>> s.loc[1, :]
b
1    a
2    b
Name: c, dtype: object
>>> 
>>> s.loc[(1, slice(None))]
b
1    a
2    b
Name: c, dtype: object
>>> 
>>> s.loc[
...     (1, slice(None)),
... ]
a  b
1  1    a
   2    b
Name: c, dtype: object
>>> 
>>> s.loc[(1, slice(None)), :]
Traceback (most recent call last):
...
pandas.core.indexing.IndexingError: Too many indexers

and for DataFrame too

>>> s.to_frame().loc[1]
   c
b   
1  a
2  b
>>> 
>>> s.to_frame().loc[1, :]
   c
b   
1  a
2  b
>>> 
>>> s.to_frame().loc[(1, slice(None))]
   c
b   
1  a
2  b
>>> 
>>> s.to_frame().loc[
...     (1, slice(None)),
... ]
     c
a b   
1 1  a
  2  b
>>> 
>>> s.to_frame().loc[(1, slice(None)), :]
     c
a b   
1 1  a
  2  b

so it appears that you can still get the old behavior with s.loc[(1, slice(None)),] for now.

There was a test added, test_loc_drops_level in #42435 to test for this new behavior, so I think we can just close this issue?

@simonjayhawkins simonjayhawkins added Closing Candidate May be closeable, needs more eyeballs and removed Usage Question Needs Info Clarification about behavior needed to assess issue labels Apr 10, 2022
@simonjayhawkins simonjayhawkins added this to the No action milestone Apr 10, 2022
@rhshadrach
Copy link
Member

Thanks for tracking that down @simonjayhawkins; @galipremsagar - sorry, I didn't notice you pointed to two different versions of pandas in the OP.

As an alternative to maintain the level, one can use .xs, e.g.

s = pd.DataFrame(
    {"a": [1, 1, 2], "b": [1, 2, 3], "c": ["a", "b", "c"]}
).set_index(["a", "b"])["c"]
print(s.xs(1, level=0, drop_level=False))

a  b
1  1    a
   2    b
Name: c, dtype: object

Does that work for your use case?

@phofl
Copy link
Member

phofl commented Apr 22, 2022

Yep, I agree that the behavior is more consistent now. Although the last two cases from @simonjayhawkins are still unfortunate

@simonjayhawkins
Copy link
Member

Although the last two cases from @simonjayhawkins are still unfortunate

let's leave issue open for now.

@simonjayhawkins simonjayhawkins removed the Closing Candidate May be closeable, needs more eyeballs label May 20, 2022
@simonjayhawkins simonjayhawkins modified the milestones: No action, Contributions Welcome May 20, 2022
@mroeschke mroeschke added the Bug label Jul 6, 2022
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

No branches or pull requests

6 participants