|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from pandas import DataFrame |
| 6 | +import pandas._testing as tm |
| 7 | + |
| 8 | + |
| 9 | +class TestDataFrameFilter: |
| 10 | + def test_filter(self, float_frame, float_string_frame): |
| 11 | + # Items |
| 12 | + filtered = float_frame.filter(["A", "B", "E"]) |
| 13 | + assert len(filtered.columns) == 2 |
| 14 | + assert "E" not in filtered |
| 15 | + |
| 16 | + filtered = float_frame.filter(["A", "B", "E"], axis="columns") |
| 17 | + assert len(filtered.columns) == 2 |
| 18 | + assert "E" not in filtered |
| 19 | + |
| 20 | + # Other axis |
| 21 | + idx = float_frame.index[0:4] |
| 22 | + filtered = float_frame.filter(idx, axis="index") |
| 23 | + expected = float_frame.reindex(index=idx) |
| 24 | + tm.assert_frame_equal(filtered, expected) |
| 25 | + |
| 26 | + # like |
| 27 | + fcopy = float_frame.copy() |
| 28 | + fcopy["AA"] = 1 |
| 29 | + |
| 30 | + filtered = fcopy.filter(like="A") |
| 31 | + assert len(filtered.columns) == 2 |
| 32 | + assert "AA" in filtered |
| 33 | + |
| 34 | + # like with ints in column names |
| 35 | + df = DataFrame(0.0, index=[0, 1, 2], columns=[0, 1, "_A", "_B"]) |
| 36 | + filtered = df.filter(like="_") |
| 37 | + assert len(filtered.columns) == 2 |
| 38 | + |
| 39 | + # regex with ints in column names |
| 40 | + # from PR #10384 |
| 41 | + df = DataFrame(0.0, index=[0, 1, 2], columns=["A1", 1, "B", 2, "C"]) |
| 42 | + expected = DataFrame( |
| 43 | + 0.0, index=[0, 1, 2], columns=pd.Index([1, 2], dtype=object) |
| 44 | + ) |
| 45 | + filtered = df.filter(regex="^[0-9]+$") |
| 46 | + tm.assert_frame_equal(filtered, expected) |
| 47 | + |
| 48 | + expected = DataFrame(0.0, index=[0, 1, 2], columns=[0, "0", 1, "1"]) |
| 49 | + # shouldn't remove anything |
| 50 | + filtered = expected.filter(regex="^[0-9]+$") |
| 51 | + tm.assert_frame_equal(filtered, expected) |
| 52 | + |
| 53 | + # pass in None |
| 54 | + with pytest.raises(TypeError, match="Must pass"): |
| 55 | + float_frame.filter() |
| 56 | + with pytest.raises(TypeError, match="Must pass"): |
| 57 | + float_frame.filter(items=None) |
| 58 | + with pytest.raises(TypeError, match="Must pass"): |
| 59 | + float_frame.filter(axis=1) |
| 60 | + |
| 61 | + # test mutually exclusive arguments |
| 62 | + with pytest.raises(TypeError, match="mutually exclusive"): |
| 63 | + float_frame.filter(items=["one", "three"], regex="e$", like="bbi") |
| 64 | + with pytest.raises(TypeError, match="mutually exclusive"): |
| 65 | + float_frame.filter(items=["one", "three"], regex="e$", axis=1) |
| 66 | + with pytest.raises(TypeError, match="mutually exclusive"): |
| 67 | + float_frame.filter(items=["one", "three"], regex="e$") |
| 68 | + with pytest.raises(TypeError, match="mutually exclusive"): |
| 69 | + float_frame.filter(items=["one", "three"], like="bbi", axis=0) |
| 70 | + with pytest.raises(TypeError, match="mutually exclusive"): |
| 71 | + float_frame.filter(items=["one", "three"], like="bbi") |
| 72 | + |
| 73 | + # objects |
| 74 | + filtered = float_string_frame.filter(like="foo") |
| 75 | + assert "foo" in filtered |
| 76 | + |
| 77 | + # unicode columns, won't ascii-encode |
| 78 | + df = float_frame.rename(columns={"B": "\u2202"}) |
| 79 | + filtered = df.filter(like="C") |
| 80 | + assert "C" in filtered |
| 81 | + |
| 82 | + def test_filter_regex_search(self, float_frame): |
| 83 | + fcopy = float_frame.copy() |
| 84 | + fcopy["AA"] = 1 |
| 85 | + |
| 86 | + # regex |
| 87 | + filtered = fcopy.filter(regex="[A]+") |
| 88 | + assert len(filtered.columns) == 2 |
| 89 | + assert "AA" in filtered |
| 90 | + |
| 91 | + # doesn't have to be at beginning |
| 92 | + df = DataFrame( |
| 93 | + {"aBBa": [1, 2], "BBaBB": [1, 2], "aCCa": [1, 2], "aCCaBB": [1, 2]} |
| 94 | + ) |
| 95 | + |
| 96 | + result = df.filter(regex="BB") |
| 97 | + exp = df[[x for x in df.columns if "BB" in x]] |
| 98 | + tm.assert_frame_equal(result, exp) |
| 99 | + |
| 100 | + @pytest.mark.parametrize( |
| 101 | + "name,expected", |
| 102 | + [ |
| 103 | + ("a", DataFrame({"a": [1, 2]})), |
| 104 | + ("a", DataFrame({"a": [1, 2]})), |
| 105 | + ("あ", DataFrame({"あ": [3, 4]})), |
| 106 | + ], |
| 107 | + ) |
| 108 | + def test_filter_unicode(self, name, expected): |
| 109 | + # GH13101 |
| 110 | + df = DataFrame({"a": [1, 2], "あ": [3, 4]}) |
| 111 | + |
| 112 | + tm.assert_frame_equal(df.filter(like=name), expected) |
| 113 | + tm.assert_frame_equal(df.filter(regex=name), expected) |
| 114 | + |
| 115 | + @pytest.mark.parametrize("name", ["a", "a"]) |
| 116 | + def test_filter_bytestring(self, name): |
| 117 | + # GH13101 |
| 118 | + df = DataFrame({b"a": [1, 2], b"b": [3, 4]}) |
| 119 | + expected = DataFrame({b"a": [1, 2]}) |
| 120 | + |
| 121 | + tm.assert_frame_equal(df.filter(like=name), expected) |
| 122 | + tm.assert_frame_equal(df.filter(regex=name), expected) |
| 123 | + |
| 124 | + def test_filter_corner(self): |
| 125 | + empty = DataFrame() |
| 126 | + |
| 127 | + result = empty.filter([]) |
| 128 | + tm.assert_frame_equal(result, empty) |
| 129 | + |
| 130 | + result = empty.filter(like="foo") |
| 131 | + tm.assert_frame_equal(result, empty) |
| 132 | + |
| 133 | + def test_filter_regex_non_string(self): |
| 134 | + # GH#5798 trying to filter on non-string columns should drop, |
| 135 | + # not raise |
| 136 | + df = pd.DataFrame(np.random.random((3, 2)), columns=["STRING", 123]) |
| 137 | + result = df.filter(regex="STRING") |
| 138 | + expected = df[["STRING"]] |
| 139 | + tm.assert_frame_equal(result, expected) |
0 commit comments