From 08e87d859f1999f722654d83d59fb9b6e1a2af0f Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 18 Sep 2021 16:20:02 +0200 Subject: [PATCH 1/2] fix bug --- pandas/io/formats/style_render.py | 39 ++++++++++--------- .../tests/io/formats/style/test_to_latex.py | 2 +- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 0ec6a9b470b50..94df9e36f89f7 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -621,28 +621,29 @@ def _translate_latex(self, d: dict) -> None: ] body = [] for r, row in enumerate(d["body"]): - if all(self.hide_index_): - row_body_headers = [] - else: - row_body_headers = [ - { - **col, - "display_value": col["display_value"] - if col["is_visible"] - else "", - "cellstyle": self.ctx_index[r, c] if col["is_visible"] else [], - } + if r not in self.hidden_rows: + if all(self.hide_index_): + row_body_headers = [] + else: + row_body_headers = [ + { + **col, + "display_value": col["display_value"] + if col["is_visible"] + else "", + "cellstyle": self.ctx_index[r, c], + } + for c, col in enumerate(row) + if col["type"] == "th" + ] + + row_body_cells = [ + {**col, "cellstyle": self.ctx[r, c - self.data.index.nlevels]} for c, col in enumerate(row) - if col["type"] == "th" + if (col["is_visible"] and col["type"] == "td") ] - row_body_cells = [ - {**col, "cellstyle": self.ctx[r, c - self.data.index.nlevels]} - for c, col in enumerate(row) - if (col["is_visible"] and col["type"] == "td") - ] - - body.append(row_body_headers + row_body_cells) + body.append(row_body_headers + row_body_cells) d["body"] = body def format( diff --git a/pandas/tests/io/formats/style/test_to_latex.py b/pandas/tests/io/formats/style/test_to_latex.py index 40ba3ca26afa4..2e28e0925ac38 100644 --- a/pandas/tests/io/formats/style/test_to_latex.py +++ b/pandas/tests/io/formats/style/test_to_latex.py @@ -752,7 +752,7 @@ def test_apply_map_header_render_mi(df_ext, index, columns, siunitx): expected_index = dedent( """\ \\multirow[c]{2}{*}{\\bfseries{A}} & a & 0 & -0.610000 & ab \\\\ - & b & 1 & -1.220000 & cd \\\\ + \\bfseries{} & b & 1 & -1.220000 & cd \\\\ B & \\bfseries{c} & 2 & -2.220000 & de \\\\ """ ) From cd4a624d8a5f33b53c8c00c48ea23b4bc8df6bdd Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 20 Oct 2021 18:04:14 +0200 Subject: [PATCH 2/2] add functionality --- pandas/io/formats/style_render.py | 2 +- pandas/tests/io/formats/style/test_to_latex.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 2c862f2f4ed96..27a5170e48949 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -740,7 +740,7 @@ def _translate_latex(self, d: dict) -> None: "display_value": col["display_value"] if col["is_visible"] else "", - "cellstyle": self.ctx_index[r, c] if col["is_visible"] else [], + "cellstyle": self.ctx_index[r, c], } for c, col in enumerate(row[:index_levels]) if (col["type"] == "th" and not self.hide_index_[c]) diff --git a/pandas/tests/io/formats/style/test_to_latex.py b/pandas/tests/io/formats/style/test_to_latex.py index 5ebe5bc101c6b..f513b0d8a197e 100644 --- a/pandas/tests/io/formats/style/test_to_latex.py +++ b/pandas/tests/io/formats/style/test_to_latex.py @@ -840,7 +840,7 @@ def test_latex_hiding_index_columns_multiindex_alignment(): 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} j2 & 13 & 14 & 15 \\\\ + \\color{blue} & \\color{blue} j2 & 13 & 14 & 15 \\\\ \\end{tabular} """ )