Skip to content

BUG: XS for multiple keys in combination with drop_level=False #41760

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
JimZwartveld opened this issue Jun 1, 2021 · 3 comments · Fixed by #41789
Closed

BUG: XS for multiple keys in combination with drop_level=False #41760

JimZwartveld opened this issue Jun 1, 2021 · 3 comments · Fixed by #41789
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@JimZwartveld
Copy link

JimZwartveld commented Jun 1, 2021

I believe a bug was introduced in the update of xs in pandas 1.2.0. The problem occurs when requesting multiple keys in combination with drop_level=False. I use the following code to highlight the problem I face:

import pandas as pd

d = [[4, 4, 2, 2, 8], [0, 0, 2, 2, 0]]

tuples = [('mammal', 'cat'), ('mammal', 'dog'), ('bird', 'penguin'), ('bird', 'pidgeon'), ('insect', 'spider')]
columns = pd.MultiIndex.from_tuples(tuples)

df = pd.DataFrame(data=d, columns=columns, index=['num_legs', 'num_wings'])
print(df.xs(['mammal', 'bird'], drop_level=False, axis=1))

In pandas 1.1.5, the following output is generated (as expected):

          mammal        bird        
             cat dog penguin pidgeon
num_legs       4   4       2       2
num_wings      0   0       2       2

While in pandas 1.2.4, an exception is raised:

Traceback (most recent call last):
  File "C:\python\miniconda3\envs\pandas_12\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 4554, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 4562, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'bird'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "pandas\_libs\index.pyx", line 705, in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc
  File "C:\python\miniconda3\envs\pandas_12\lib\site-packages\pandas\core\indexes\base.py", line 3082, in get_loc
    raise KeyError(key) from err
KeyError: 'bird'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\python\miniconda3\envs\pandas_12\lib\site-packages\pandas\core\indexes\multi.py", line 3036, in _get_loc_level
    return (self._engine.get_loc(key), None)
  File "pandas\_libs\index.pyx", line 708, in pandas._libs.index.BaseMultiIndexCodesEngine.get_loc
KeyError: ('mammal', 'bird')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\jimzw\AppData\Roaming\JetBrains\PyCharmCE2021.1\scratches\scratch_4.py", line 9, in <module>
    print(df.xs(['mammal', 'bird'], drop_level=False, axis=1))
  File "C:\python\miniconda3\envs\pandas_12\lib\site-packages\pandas\core\generic.py", line 3733, in xs
    loc, new_index = index._get_loc_level(
  File "C:\python\miniconda3\envs\pandas_12\lib\site-packages\pandas\core\indexes\multi.py", line 3038, in _get_loc_level
    raise KeyError(key) from e
KeyError: ('mammal', 'bird')
@JimZwartveld JimZwartveld added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 1, 2021
@phofl
Copy link
Member

phofl commented Jun 2, 2021

Hi, thanks for your Report.

Looks like a bug.

Could you try to narrow your example down a bit? Removing transpose and maybe defining the index explicitly?

@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 2, 2021
@simonjayhawkins simonjayhawkins added this to the 1.2.5 milestone Jun 3, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Jun 3, 2021
@simonjayhawkins
Copy link
Member

I believe a bug was introduced in the update of xs in pandas 1.2.0.

first bad commit: [5c111cb] BUG: Bug in xs ignored droplevel (#37776)

@JimZwartveld
Copy link
Author

@phofl I updated the example, hope this is what you were looking for.

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 Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants