|
| 1 | +import pytest |
| 2 | + |
| 3 | +from pandas import ( |
| 4 | + DataFrame, |
| 5 | + IndexSlice, |
| 6 | +) |
| 7 | + |
| 8 | +pytest.importorskip("jinja2") |
| 9 | + |
| 10 | +from pandas.io.formats.style import Styler |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def df(): |
| 15 | + return DataFrame( |
| 16 | + [[1, 2, 3], [4, 5, 6], [7, 8, 9]], |
| 17 | + index=["i", "j", "j"], |
| 18 | + columns=["c", "d", "d"], |
| 19 | + dtype=float, |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def styler(df): |
| 25 | + return Styler(df, uuid_len=0) |
| 26 | + |
| 27 | + |
| 28 | +def test_format_non_unique(df): |
| 29 | + # GH 41269 |
| 30 | + |
| 31 | + # test dict |
| 32 | + html = df.style.format({"d": "{:.1f}"}).render() |
| 33 | + for val in ["1.000000<", "4.000000<", "7.000000<"]: |
| 34 | + assert val in html |
| 35 | + for val in ["2.0<", "3.0<", "5.0<", "6.0<", "8.0<", "9.0<"]: |
| 36 | + assert val in html |
| 37 | + |
| 38 | + # test subset |
| 39 | + html = df.style.format(precision=1, subset=IndexSlice["j", "d"]).render() |
| 40 | + for val in ["1.000000<", "4.000000<", "7.000000<", "2.000000<", "3.000000<"]: |
| 41 | + assert val in html |
| 42 | + for val in ["5.0<", "6.0<", "8.0<", "9.0<"]: |
| 43 | + assert val in html |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize("func", ["apply", "applymap"]) |
| 47 | +def test_apply_applymap_non_unique_raises(df, func): |
| 48 | + # GH 41269 |
| 49 | + if func == "apply": |
| 50 | + op = lambda s: ["color: red;"] * len(s) |
| 51 | + else: |
| 52 | + op = lambda v: "color: red;" |
| 53 | + |
| 54 | + with pytest.raises(KeyError, match="`Styler.apply` and `.applymap` are not"): |
| 55 | + getattr(df.style, func)(op)._compute() |
| 56 | + |
| 57 | + |
| 58 | +def test_table_styles_dict_non_unique_index(styler): |
| 59 | + styles = styler.set_table_styles( |
| 60 | + {"j": [{"selector": "td", "props": "a: v;"}]}, axis=1 |
| 61 | + ).table_styles |
| 62 | + assert styles == [ |
| 63 | + {"selector": "td.row1", "props": [("a", "v")]}, |
| 64 | + {"selector": "td.row2", "props": [("a", "v")]}, |
| 65 | + ] |
| 66 | + |
| 67 | + |
| 68 | +def test_table_styles_dict_non_unique_columns(styler): |
| 69 | + styles = styler.set_table_styles( |
| 70 | + {"d": [{"selector": "td", "props": "a: v;"}]}, axis=0 |
| 71 | + ).table_styles |
| 72 | + assert styles == [ |
| 73 | + {"selector": "td.col1", "props": [("a", "v")]}, |
| 74 | + {"selector": "td.col2", "props": [("a", "v")]}, |
| 75 | + ] |
| 76 | + |
| 77 | + |
| 78 | +def test_tooltips_non_unique_raises(styler): |
| 79 | + # ttips has unique keys |
| 80 | + ttips = DataFrame([["1", "2"], ["3", "4"]], columns=["c", "d"], index=["a", "b"]) |
| 81 | + styler.set_tooltips(ttips=ttips) # OK |
| 82 | + |
| 83 | + # ttips has non-unique columns |
| 84 | + ttips = DataFrame([["1", "2"], ["3", "4"]], columns=["c", "c"], index=["a", "b"]) |
| 85 | + with pytest.raises(KeyError, match="Tooltips render only if `ttips` has unique"): |
| 86 | + styler.set_tooltips(ttips=ttips) |
| 87 | + |
| 88 | + # ttips has non-unique index |
| 89 | + ttips = DataFrame([["1", "2"], ["3", "4"]], columns=["c", "d"], index=["a", "a"]) |
| 90 | + with pytest.raises(KeyError, match="Tooltips render only if `ttips` has unique"): |
| 91 | + styler.set_tooltips(ttips=ttips) |
| 92 | + |
| 93 | + |
| 94 | +def test_set_td_classes_non_unique_raises(styler): |
| 95 | + # classes has unique keys |
| 96 | + classes = DataFrame([["1", "2"], ["3", "4"]], columns=["c", "d"], index=["a", "b"]) |
| 97 | + styler.set_td_classes(classes=classes) # OK |
| 98 | + |
| 99 | + # classes has non-unique columns |
| 100 | + classes = DataFrame([["1", "2"], ["3", "4"]], columns=["c", "c"], index=["a", "b"]) |
| 101 | + with pytest.raises(KeyError, match="Classes render only if `classes` has unique"): |
| 102 | + styler.set_td_classes(classes=classes) |
| 103 | + |
| 104 | + # classes has non-unique index |
| 105 | + classes = DataFrame([["1", "2"], ["3", "4"]], columns=["c", "d"], index=["a", "a"]) |
| 106 | + with pytest.raises(KeyError, match="Classes render only if `classes` has unique"): |
| 107 | + styler.set_td_classes(classes=classes) |
| 108 | + |
| 109 | + |
| 110 | +def test_hide_columns_non_unique(styler): |
| 111 | + ctx = styler.hide_columns(["d"])._translate() |
| 112 | + |
| 113 | + assert ctx["head"][0][1]["display_value"] == "c" |
| 114 | + assert ctx["head"][0][1]["is_visible"] is True |
| 115 | + |
| 116 | + assert ctx["head"][0][2]["display_value"] == "d" |
| 117 | + assert ctx["head"][0][2]["is_visible"] is False |
| 118 | + |
| 119 | + assert ctx["head"][0][3]["display_value"] == "d" |
| 120 | + assert ctx["head"][0][3]["is_visible"] is False |
| 121 | + |
| 122 | + assert ctx["body"][0][1]["is_visible"] is True |
| 123 | + assert ctx["body"][0][2]["is_visible"] is False |
| 124 | + assert ctx["body"][0][3]["is_visible"] is False |
0 commit comments