Skip to content

BUG: apply_index styles not universally applied across LaTeX multirows #41994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
attack68 opened this issue Jun 14, 2021 · 3 comments
Closed
Labels
Closing Candidate May be closeable, needs more eyeballs IO LaTeX to_latex Styler conditional formatting using DataFrame.style

Comments

@attack68
Copy link
Contributor

attack68 commented Jun 14, 2021

PR #41993 makes Styler.applymap_index and Styler.apply_index compatible with to_latex. But, with MultiIndexes this introduces a bug: cell coloring is not universal for multirow, albeit multicols are OK.

df = pd.DataFrame({"A": [0, 1, 2], "B": [-0.61, -1.22, -2.22], "C": ["ab", "cd", "de"]})
cidx = pd.MultiIndex.from_tuples([("Z", "a"), ("Z", "b"), ("Y", "c")])
ridx = pd.MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("B", "c")])
df.index, df.columns = ridx, cidx
styler = df.style
func = lambda v: 'cellcolor:{red}; color:{white}; bfseries:--rwrap;' if "A" in v or "Z" in v or "c" in v else None
print(styler.applymap_header(func, axis="columns").applymap_header(func).to_latex(hrules=True))
\begin{tabular}{llrrl}
\toprule
{} & {} & \multicolumn{2}{r}{\cellcolor{red} \color{white} \bfseries{Z}} & {Y} \\
{} & {} & {a} & {b} & {\cellcolor{red} \color{white} \bfseries{c}} \\
\midrule
\multirow[c]{2}{*}{\cellcolor{red} \color{white} \bfseries{A}} & a & 0 & -0.610000 & ab \\
 & b & 1 & -1.220000 & cd \\
B & \cellcolor{red} \color{white} \bfseries{c} & 2 & -2.220000 & de \\
\bottomrule
\end{tabular}

Screen Shot 2021-06-14 at 09 37 21

Even if the code is amended to print the styles in problem cells, albeit with a blank display value the problem is not solved because the text is overwritten:

\begin{tabular}{llrrl}
\toprule
{} & {} & \multicolumn{2}{r}{\cellcolor{red} \color{white} \bfseries{Z}} & {Y} \\
{} & {} & {a} & {b} & {\cellcolor{red} \color{white} \bfseries{c}} \\
\midrule
\multirow[c]{2}{*}{\cellcolor{red} \color{white} \bfseries{A}} & a & 0 & -0.610000 & ab \\
\cellcolor{red} \color{white} \bfseries{} & b & 1 & -1.220000 & cd \\
B & \cellcolor{red} \color{white} \bfseries{c} & 2 & -2.220000 & de \\
\bottomrule
\end{tabular}

Screen Shot 2021-06-14 at 09 39 03

One solution is to apply the multirow last so that it overwrites previous rendered cells, using a negative rowspan.

\begin{tabular}{llrrl}
\toprule
{} & {} & \multicolumn{2}{r}{\cellcolor{red} \color{white} \bfseries{Z}} & {Y} \\
{} & {} & {a} & {b} & {\cellcolor{red} \color{white} \bfseries{c}} \\
\midrule
\cellcolor{red} \color{white} \bfseries{} & a & 0 & -0.610000 & ab \\
\multirow[c]{-2}{*}{\cellcolor{red} \color{white} \bfseries{A}} & b & 1 & -1.220000 & cd \\
B & \cellcolor{red} \color{white} \bfseries{c} & 2 & -2.220000 & de \\
\bottomrule
\end{tabular}

Screen Shot 2021-06-14 at 09 41 13

Albeit this might be quite difficult to code and have other repercussions.

@attack68 attack68 added Bug IO LaTeX to_latex Styler conditional formatting using DataFrame.style labels Jun 14, 2021
@attack68 attack68 changed the title BUG: apply_header styles not universally applied across multirows BUG: apply_header styles not universally applied across LaTeX multirows Jun 14, 2021
@attack68 attack68 changed the title BUG: apply_header styles not universally applied across LaTeX multirows BUG: apply_index styles not universally applied across LaTeX multirows Aug 12, 2021
@attack68
Copy link
Contributor Author

An alternative to this is to solve it with naive multirow and ensure blank cells are still colored.

@attack68
Copy link
Contributor Author

With the above PR and multicol_align="naive" or multicol_align="t" we can obtain somewhat fudged but correct results:

with "naive"

print(styler.applymap_index(func, axis="columns").applymap_index(func).to_latex(hrules=True, multirow_align="naive"))

\begin{tabular}{llrrl}
\toprule
 &  & \multicolumn{2}{r}{\cellcolor{red} \color{white} \bfseries{Z}} & Y \\
 &  & a & b & \cellcolor{red} \color{white} \bfseries{c} \\
\midrule
\cellcolor{red} \color{white} \bfseries{A} & a & 0 & -0.610000 & ab \\
\cellcolor{red} \color{white} \bfseries{} & b & 1 & -1.220000 & cd \\
B & \cellcolor{red} \color{white} \bfseries{c} & 2 & -2.220000 & de \\
\bottomrule
\end{tabular}

Screen Shot 2021-10-20 at 18 23 19

with "t"

print(styler.applymap_index(func, axis="columns").applymap_index(func).to_latex(hrules=True, multirow_align="t"))

\begin{tabular}{llrrl}
\toprule
 &  & \multicolumn{2}{r}{\cellcolor{red} \color{white} \bfseries{Z}} & Y \\
 &  & a & b & \cellcolor{red} \color{white} \bfseries{c} \\
\midrule
\multirow[t]{2}{*}{\cellcolor{red} \color{white} \bfseries{A}} & a & 0 & -0.610000 & ab \\
\cellcolor{red} \color{white} \bfseries{} & b & 1 & -1.220000 & cd \\
B & \cellcolor{red} \color{white} \bfseries{c} & 2 & -2.220000 & de \\
\bottomrule
\end{tabular}

Screen Shot 2021-10-20 at 18 24 37

@attack68 attack68 added Closing Candidate May be closeable, needs more eyeballs and removed Bug labels Oct 26, 2021
@attack68
Copy link
Contributor Author

closing as non-actionable for now, with at least a work around in place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closing Candidate May be closeable, needs more eyeballs IO LaTeX to_latex Styler conditional formatting using DataFrame.style
Projects
None yet
Development

No branches or pull requests

1 participant