Closed
Description
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')