def test_latex_hiding_index_columns_multiindex_alignment(): # gh 43644 midx = MultiIndex.from_product([["i0", "j0"], ["i1"], ["i2", "j2"]], names=["i-0", "i-1", "i-2"]) cidx = MultiIndex.from_product([["c0"], ["c1", "d1"], ["c2", "d2"]], names=["c-0", "c-1", "c-2"]) df = DataFrame(np.arange(16).reshape(4, 4), index=midx, columns=cidx) styler = Styler(df, uuid_len=0) styler.hide_index(level=1).hide_columns(level=0) styler.hide_index([("i0", "i1", "i2")]) styler.hide_columns([("c0", "c1", "c2")]) styler.applymap(lambda x: "color:{red};" if x == 5 else "") styler.applymap_index(lambda x: "color:{blue};" if "j" in x else "") result = styler.to_latex() expected = dedent("""\ \\begin{tabular}{llrrr} & c-1 & c1 & \\multicolumn{2}{r}{d1} \\\\ & c-2 & d2 & c2 & d2 \\\\ i-0 & i-2 & & & \\\\ i0 & \\color{blue} j2 & \\color{red} 5 & 6 & 7 \\\\ \\multirow[c]{2}{*}{\\color{blue} j0} & i2 & 9 & 10 & 11 \\\\ \\color{blue} & \\color{blue} j2 & 13 & 14 & 15 \\\\ \\end{tabular} """) assert result == expected
def test_replaced_css_class_names(styler_mi): css = { "row_heading": "ROWHEAD", # "col_heading": "COLHEAD", "index_name": "IDXNAME", # "col": "COL", "row": "ROW", # "col_trim": "COLTRIM", "row_trim": "ROWTRIM", "level": "LEVEL", "data": "DATA", "blank": "BLANK", } midx = MultiIndex.from_product([["a", "b"], ["c", "d"]]) styler_mi = Styler( DataFrame(np.arange(16).reshape(4, 4), index=midx, columns=midx), uuid_len=0, ).set_table_styles(css_class_names=css) styler_mi.index.names = ["n1", "n2"] styler_mi.hide(styler_mi.index[1:], axis=0) styler_mi.hide(styler_mi.columns[1:], axis=1) styler_mi.applymap_index(lambda v: "color: red;", axis=0) styler_mi.applymap_index(lambda v: "color: green;", axis=1) styler_mi.applymap(lambda v: "color: blue;") expected = dedent("""\ <style type="text/css"> #T__ROW0_col0 { color: blue; } #T__LEVEL0_ROW0, #T__LEVEL1_ROW0 { color: red; } #T__LEVEL0_col0, #T__LEVEL1_col0 { color: green; } </style> <table id="T_"> <thead> <tr> <th class="BLANK" > </th> <th class="IDXNAME LEVEL0" >n1</th> <th id="T__LEVEL0_col0" class="col_heading LEVEL0 col0" >a</th> </tr> <tr> <th class="BLANK" > </th> <th class="IDXNAME LEVEL1" >n2</th> <th id="T__LEVEL1_col0" class="col_heading LEVEL1 col0" >c</th> </tr> <tr> <th class="IDXNAME LEVEL0" >n1</th> <th class="IDXNAME LEVEL1" >n2</th> <th class="BLANK col0" > </th> </tr> </thead> <tbody> <tr> <th id="T__LEVEL0_ROW0" class="ROWHEAD LEVEL0 ROW0" >a</th> <th id="T__LEVEL1_ROW0" class="ROWHEAD LEVEL1 ROW0" >c</th> <td id="T__ROW0_col0" class="DATA ROW0 col0" >0</td> </tr> </tbody> </table> """) result = styler_mi.to_html() assert result == expected