diff --git a/pandas/tests/io/formats/style/test_highlight.py b/pandas/tests/io/formats/style/test_highlight.py index 7cf958be11392..8aca3cadff0b4 100644 --- a/pandas/tests/io/formats/style/test_highlight.py +++ b/pandas/tests/io/formats/style/test_highlight.py @@ -5,61 +5,68 @@ pytest.importorskip("jinja2") +from pandas.io.formats.style import Styler -class TestStylerHighlight: - def setup_method(self, method): - np.random.seed(24) - self.s = DataFrame({"A": np.random.permutation(range(6))}) - self.df = DataFrame({"A": [0, 1], "B": np.random.randn(2)}) - def test_highlight_null(self): - df = DataFrame({"A": [0, np.nan]}) - result = df.style.highlight_null()._compute().ctx - expected = {(1, 0): [("background-color", "red")]} - assert result == expected +@pytest.fixture +def df(): + return DataFrame({"A": [0, np.nan, 10], "B": [1, None, 2]}) - def test_highlight_null_subset(self): - # GH 31345 - df = DataFrame({"A": [0, np.nan], "B": [0, np.nan]}) - result = ( - df.style.highlight_null(null_color="red", subset=["A"]) - .highlight_null(null_color="green", subset=["B"]) - ._compute() - .ctx - ) - expected = { - (1, 0): [("background-color", "red")], - (1, 1): [("background-color", "green")], - } - assert result == expected - @pytest.mark.parametrize("f", ["highlight_min", "highlight_max"]) - def test_highlight_minmax_basic(self, f): - expected = { - (0, 0): [("background-color", "red")], - (1, 0): [("background-color", "red")], - } - if f == "highlight_min": - df = -self.df - else: - df = self.df - result = getattr(df.style, f)(axis=1, color="red")._compute().ctx - assert result == expected +@pytest.fixture +def styler(df): + return Styler(df, uuid_len=0) - @pytest.mark.parametrize("f", ["highlight_min", "highlight_max"]) - @pytest.mark.parametrize( - "kwargs", - [ - {"axis": None, "color": "red"}, # test axis - {"axis": 0, "subset": ["A"], "color": "red"}, # test subset - {"axis": None, "props": "background-color: red"}, # test props - ], + +def test_highlight_null(styler): + result = styler.highlight_null()._compute().ctx + expected = { + (1, 0): [("background-color", "red")], + (1, 1): [("background-color", "red")], + } + assert result == expected + + +def test_highlight_null_subset(styler): + # GH 31345 + result = ( + styler.highlight_null(null_color="red", subset=["A"]) + .highlight_null(null_color="green", subset=["B"]) + ._compute() + .ctx ) - def test_highlight_minmax_ext(self, f, kwargs): - expected = {(1, 0): [("background-color", "red")]} - if f == "highlight_min": - df = -self.df - else: - df = self.df - result = getattr(df.style, f)(**kwargs)._compute().ctx - assert result == expected + expected = { + (1, 0): [("background-color", "red")], + (1, 1): [("background-color", "green")], + } + assert result == expected + + +@pytest.mark.parametrize("f", ["highlight_min", "highlight_max"]) +def test_highlight_minmax_basic(df, f): + expected = { + (0, 1): [("background-color", "red")], + # ignores NaN row, + (2, 0): [("background-color", "red")], + } + if f == "highlight_min": + df = -df + result = getattr(df.style, f)(axis=1, color="red")._compute().ctx + assert result == expected + + +@pytest.mark.parametrize("f", ["highlight_min", "highlight_max"]) +@pytest.mark.parametrize( + "kwargs", + [ + {"axis": None, "color": "red"}, # test axis + {"axis": 0, "subset": ["A"], "color": "red"}, # test subset and ignores NaN + {"axis": None, "props": "background-color: red"}, # test props + ], +) +def test_highlight_minmax_ext(df, f, kwargs): + expected = {(2, 0): [("background-color", "red")]} + if f == "highlight_min": + df = -df + result = getattr(df.style, f)(**kwargs)._compute().ctx + assert result == expected