|
5 | 5 |
|
6 | 6 | pytest.importorskip("jinja2")
|
7 | 7 |
|
| 8 | +from pandas.io.formats.style import Styler |
8 | 9 |
|
9 |
| -class TestStylerHighlight: |
10 |
| - def setup_method(self, method): |
11 |
| - np.random.seed(24) |
12 |
| - self.s = DataFrame({"A": np.random.permutation(range(6))}) |
13 |
| - self.df = DataFrame({"A": [0, 1], "B": np.random.randn(2)}) |
14 | 10 |
|
15 |
| - def test_highlight_null(self): |
16 |
| - df = DataFrame({"A": [0, np.nan]}) |
17 |
| - result = df.style.highlight_null()._compute().ctx |
18 |
| - expected = {(1, 0): [("background-color", "red")]} |
19 |
| - assert result == expected |
| 11 | +@pytest.fixture |
| 12 | +def df(): |
| 13 | + return DataFrame({"A": [0, np.nan, 10], "B": [1, None, 2]}) |
20 | 14 |
|
21 |
| - def test_highlight_null_subset(self): |
22 |
| - # GH 31345 |
23 |
| - df = DataFrame({"A": [0, np.nan], "B": [0, np.nan]}) |
24 |
| - result = ( |
25 |
| - df.style.highlight_null(null_color="red", subset=["A"]) |
26 |
| - .highlight_null(null_color="green", subset=["B"]) |
27 |
| - ._compute() |
28 |
| - .ctx |
29 |
| - ) |
30 |
| - expected = { |
31 |
| - (1, 0): [("background-color", "red")], |
32 |
| - (1, 1): [("background-color", "green")], |
33 |
| - } |
34 |
| - assert result == expected |
35 | 15 |
|
36 |
| - @pytest.mark.parametrize("f", ["highlight_min", "highlight_max"]) |
37 |
| - def test_highlight_minmax_basic(self, f): |
38 |
| - expected = { |
39 |
| - (0, 0): [("background-color", "red")], |
40 |
| - (1, 0): [("background-color", "red")], |
41 |
| - } |
42 |
| - if f == "highlight_min": |
43 |
| - df = -self.df |
44 |
| - else: |
45 |
| - df = self.df |
46 |
| - result = getattr(df.style, f)(axis=1, color="red")._compute().ctx |
47 |
| - assert result == expected |
| 16 | +@pytest.fixture |
| 17 | +def styler(df): |
| 18 | + return Styler(df, uuid_len=0) |
48 | 19 |
|
49 |
| - @pytest.mark.parametrize("f", ["highlight_min", "highlight_max"]) |
50 |
| - @pytest.mark.parametrize( |
51 |
| - "kwargs", |
52 |
| - [ |
53 |
| - {"axis": None, "color": "red"}, # test axis |
54 |
| - {"axis": 0, "subset": ["A"], "color": "red"}, # test subset |
55 |
| - {"axis": None, "props": "background-color: red"}, # test props |
56 |
| - ], |
| 20 | + |
| 21 | +def test_highlight_null(styler): |
| 22 | + result = styler.highlight_null()._compute().ctx |
| 23 | + expected = { |
| 24 | + (1, 0): [("background-color", "red")], |
| 25 | + (1, 1): [("background-color", "red")], |
| 26 | + } |
| 27 | + assert result == expected |
| 28 | + |
| 29 | + |
| 30 | +def test_highlight_null_subset(styler): |
| 31 | + # GH 31345 |
| 32 | + result = ( |
| 33 | + styler.highlight_null(null_color="red", subset=["A"]) |
| 34 | + .highlight_null(null_color="green", subset=["B"]) |
| 35 | + ._compute() |
| 36 | + .ctx |
57 | 37 | )
|
58 |
| - def test_highlight_minmax_ext(self, f, kwargs): |
59 |
| - expected = {(1, 0): [("background-color", "red")]} |
60 |
| - if f == "highlight_min": |
61 |
| - df = -self.df |
62 |
| - else: |
63 |
| - df = self.df |
64 |
| - result = getattr(df.style, f)(**kwargs)._compute().ctx |
65 |
| - assert result == expected |
| 38 | + expected = { |
| 39 | + (1, 0): [("background-color", "red")], |
| 40 | + (1, 1): [("background-color", "green")], |
| 41 | + } |
| 42 | + assert result == expected |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize("f", ["highlight_min", "highlight_max"]) |
| 46 | +def test_highlight_minmax_basic(df, f): |
| 47 | + expected = { |
| 48 | + (0, 1): [("background-color", "red")], |
| 49 | + # ignores NaN row, |
| 50 | + (2, 0): [("background-color", "red")], |
| 51 | + } |
| 52 | + if f == "highlight_min": |
| 53 | + df = -df |
| 54 | + result = getattr(df.style, f)(axis=1, color="red")._compute().ctx |
| 55 | + assert result == expected |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize("f", ["highlight_min", "highlight_max"]) |
| 59 | +@pytest.mark.parametrize( |
| 60 | + "kwargs", |
| 61 | + [ |
| 62 | + {"axis": None, "color": "red"}, # test axis |
| 63 | + {"axis": 0, "subset": ["A"], "color": "red"}, # test subset and ignores NaN |
| 64 | + {"axis": None, "props": "background-color: red"}, # test props |
| 65 | + ], |
| 66 | +) |
| 67 | +def test_highlight_minmax_ext(df, f, kwargs): |
| 68 | + expected = {(2, 0): [("background-color", "red")]} |
| 69 | + if f == "highlight_min": |
| 70 | + df = -df |
| 71 | + result = getattr(df.style, f)(**kwargs)._compute().ctx |
| 72 | + assert result == expected |
0 commit comments