ENH: Styler.apply(map)_index made compatible with Styler.to_excel by attack68 · Pull Request #41995 · pandas-dev/pandas (original) (raw)

This is a follow-on to #41893.

It makes the new Styler.apply_index and Styler.applymap_index methods compatible with Styler.to_excel. If the methods are not used the output reverts to the old format.

Screen Shot 2021-06-14 at 10 58 47

MultiIndex Case

midx = pd.MultiIndex.from_product([['ix', 'jy'], [0, 1], ['x3', 'z4']]) df = pd.DataFrame([np.arange(8)], columns=midx) def highlight_x(s): return ["background-color: yellow;" if 'x' in v else "" for v in s] df.style.apply_index(highlight_x, axis="columns", level=[0, 2]).to_excel("mi_test.xlsx")

Screen Shot 2021-08-12 at 09 24 15

NOTE on TESTS

The tests for styler_to_excel were broken and were xfailed out of production in 2019. Since they dont work I have replaced them with some other, cleaner, tests and add tests relevant to this PR also.

Closes #25351