Skip to content

Commit

Permalink
Backport PR #54982 on branch 2.1.x (REG: filter not respecting the or…
Browse files Browse the repository at this point in the history
…der of labels) (#55022)

Backport PR #54982: REG: filter not respecting the order of labels

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed Sep 6, 2023
1 parent b736912 commit a6445df
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
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 @@ -19,6 +19,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)

0 comments on commit a6445df

Please sign in to comment.