Skip to content

Backport PR #54982 on branch 2.1.x (REG: filter not respecting the order of labels) #55022

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
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Fixed regressions
- Fixed regression in :func:`read_csv` when ``delim_whitespace`` is True (:issue:`54918`, :issue:`54931`)
- Fixed regression in :meth:`.GroupBy.get_group` raising for ``axis=1`` (:issue:`54858`)
- Fixed regression in :meth:`DataFrame.__setitem__` raising ``AssertionError`` when setting a :class:`Series` with a partial :class:`MultiIndex` (:issue:`54875`)
- Fixed regression in :meth:`DataFrame.filter` not respecting the order of elements for ``filter`` (:issue:`54980`)
- Fixed regression in :meth:`DataFrame.to_sql` not roundtripping datetime columns correctly for sqlite (:issue:`54877`)
- Fixed regression in :meth:`MultiIndex.append` raising when appending overlapping :class:`IntervalIndex` levels (:issue:`54934`)
- Fixed regression in :meth:`Series.drop_duplicates` for PyArrow strings (:issue:`54904`)
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5701,10 +5701,12 @@ def filter(

if items is not None:
name = self._get_axis_name(axis)
items = Index(items).intersection(labels)
if len(items) == 0:
# Keep the dtype of labels when we are empty
items = items.astype(labels.dtype)
# error: Keywords must be strings
return self.reindex( # type: ignore[misc]
**{name: labels.intersection(items)}
)
return self.reindex(**{name: items}) # type: ignore[misc]
elif like:

def f(x) -> bool_t:
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/frame/methods/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,17 @@ def test_filter_regex_non_string(self):
result = df.filter(regex="STRING")
expected = df[["STRING"]]
tm.assert_frame_equal(result, expected)

def test_filter_keep_order(self):
# GH#54980
df = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
result = df.filter(items=["B", "A"])
expected = df[["B", "A"]]
tm.assert_frame_equal(result, expected)

def test_filter_different_dtype(self):
# GH#54980
df = DataFrame({1: [1, 2, 3], 2: [4, 5, 6]})
result = df.filter(items=["B", "A"])
expected = df[[]]
tm.assert_frame_equal(result, expected)