Skip to content

CLN: clarify TypeError for IndexSlice argument to pd.xs #35411

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

Merged
merged 10 commits into from
Aug 7, 2020
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Missing
MultiIndex
^^^^^^^^^^

-
- Bug in :meth:`DataFrame.xs` when used with :class:`IndexSlice` raises ``TypeError`` with message `Expected label or tuple of labels` (:issue:`35301`)
-

I/O
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3492,7 +3492,10 @@ class animal locomotion

index = self.index
if isinstance(index, MultiIndex):
loc, new_index = self.index.get_loc_level(key, drop_level=drop_level)
try:
loc, new_index = self.index.get_loc_level(key, drop_level=drop_level)
except TypeError as e:
raise TypeError(f"Expected label or tuple of labels, got {key}") from e
else:
loc = self.index.get_loc(key)

Expand Down
23 changes: 22 additions & 1 deletion pandas/tests/indexing/multiindex/test_xs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pytest

from pandas import DataFrame, Index, MultiIndex, Series, concat, date_range
from pandas import DataFrame, Index, IndexSlice, MultiIndex, Series, concat, date_range
import pandas._testing as tm
import pandas.core.common as com

Expand Down Expand Up @@ -220,6 +220,27 @@ def test_xs_level_series_slice_not_implemented(
s[2000, 3:4]


def test_xs_IndexSlice_argument_not_implemented():
# GH 35301

index = MultiIndex(
levels=[[("foo", "bar", 0), ("foo", "baz", 0), ("foo", "qux", 0)], [0, 1]],
codes=[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]],
)

series = Series(np.random.randn(6), index=index)
frame = DataFrame(np.random.randn(6, 4), index=index)

msg = (
"Expected label or tuple of labels, got "
r"\(\('foo', 'qux', 0\), slice\(None, None, None\)\)"
)
with pytest.raises(TypeError, match=msg):
frame.xs(IndexSlice[("foo", "qux", 0), :])
with pytest.raises(TypeError, match=msg):
series.xs(IndexSlice[("foo", "qux", 0), :])


def test_series_getitem_multiindex_xs():
# GH6258
dt = list(date_range("20130903", periods=3))
Expand Down