Skip to content

Bug in xs raising KeyError for MultiIndex columns with droplevel False and list indexe #41789

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 8 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/source/whatsnew/v1.2.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ Fixed regressions

.. ---------------------------------------------------------------------------

.. _whatsnew_125.deprecations:

Deprecations
~~~~~~~~~~~~

- Deprecated passing lists as ``key`` to :meth:`DataFrame.xs` and :meth:`Series.xs` (:issue:`41760`)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can backport this since it violates our policy.

pandas will introduce deprecations in **minor** releases. These deprecations
will preserve the existing behavior while emitting a warning that provide
guidance on:

* How to achieve similar behavior if an alternative is available
* The pandas version in which the deprecation will be enforced.

We will not introduce new deprecations in patch releases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will make a pr later to move back to 1.3

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. just need to move the release note on master. I'll close the backport PR.

.. _whatsnew_125.bug_fixes:

Bug fixes
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3756,6 +3756,15 @@ class animal locomotion
"""
axis = self._get_axis_number(axis)
labels = self._get_axis(axis)

if isinstance(key, list):
warnings.warn(
"Passing lists as key for xs is deprecated and will be removed in a "
"future version. Pass key as a tuple instead.",
FutureWarning,
stacklevel=2,
)

if level is not None:
if not isinstance(labels, MultiIndex):
raise TypeError("Index must be a MultiIndex")
Expand Down
17 changes: 15 additions & 2 deletions pandas/tests/frame/indexing/test_xs.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def test_xs_keep_level(self):
expected = df[:1]
tm.assert_frame_equal(result, expected)

result = df.xs([2008, "sat"], level=["year", "day"], drop_level=False)
with tm.assert_produces_warning(FutureWarning):
result = df.xs([2008, "sat"], level=["year", "day"], drop_level=False)
tm.assert_frame_equal(result, expected)

def test_xs_view(self, using_array_manager):
Expand Down Expand Up @@ -187,7 +188,11 @@ def test_xs_with_duplicates(self, key, level, multiindex_dataframe_random_data):
assert df.index.is_unique is False
expected = concat([frame.xs("one", level="second")] * 2)

result = df.xs(key, level=level)
if isinstance(key, list):
with tm.assert_produces_warning(FutureWarning):
result = df.xs(key, level=level)
else:
result = df.xs(key, level=level)
tm.assert_frame_equal(result, expected)

def test_xs_missing_values_in_index(self):
Expand Down Expand Up @@ -358,3 +363,11 @@ def test_xs_droplevel_false_view(self, using_array_manager):
df.iloc[0, 0] = 2
expected = DataFrame({"a": [1]})
tm.assert_frame_equal(result, expected)

def test_xs_list_indexer_droplevel_false(self):
# GH#41760
mi = MultiIndex.from_tuples([("x", "m", "a"), ("x", "n", "b"), ("y", "o", "c")])
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=mi)
with tm.assert_produces_warning(FutureWarning):
with pytest.raises(KeyError, match="y"):
df.xs(["x", "y"], drop_level=False, axis=1)
3 changes: 2 additions & 1 deletion pandas/tests/indexing/multiindex/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def test_xs_partial(
)
df = DataFrame(np.random.randn(8, 4), index=index, columns=list("abcd"))

result = df.xs(["foo", "one"])
with tm.assert_produces_warning(FutureWarning):
result = df.xs(["foo", "one"])
expected = df.loc["foo", "one"]
tm.assert_frame_equal(result, expected)

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/indexing/test_xs.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,13 @@ def test_series_xs_droplevel_false(self):
),
)
tm.assert_series_equal(result, expected)

def test_xs_key_as_list(self):
# GH#41760
mi = MultiIndex.from_tuples([("a", "x")], names=["level1", "level2"])
ser = Series([1], index=mi)
with tm.assert_produces_warning(FutureWarning):
ser.xs(["a", "x"], axis=0, drop_level=False)

with tm.assert_produces_warning(FutureWarning):
ser.xs(["a"], axis=0, drop_level=False)