From 7db906a61495a5be1c0b1c60daef20c6a474e8f2 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 18 Sep 2021 16:39:30 +0200 Subject: [PATCH 1/4] fix bug --- pandas/io/formats/style_render.py | 41 ++++++++++--------- .../tests/io/formats/style/test_to_latex.py | 14 +++++++ 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 0ec6a9b470b50..48416b2a2db45 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -621,28 +621,31 @@ 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] + if col["is_visible"] + else [], + } + 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..913317e129bf9 100644 --- a/pandas/tests/io/formats/style/test_to_latex.py +++ b/pandas/tests/io/formats/style/test_to_latex.py @@ -782,3 +782,17 @@ def test_repr_option(styler): def test_siunitx_basic_headers(styler): assert "{} & {A} & {B} & {C} \\\\" in styler.to_latex(siunitx=True) assert " & A & B & C \\\\" in styler.to_latex() # default siunitx=False + + +def test_hide_index_latex(styler): + styler.hide_index([0]) + result = styler.to_latex() + expected = dedent( + """\ + \\begin{tabular}{lrrl} + & A & B & C \\\\ + 1 & 1 & -1.22 & cd \\\\ + \\end{tabular} + """ + ) + assert expected == result From ed05a90b999b5dbf64b0b0b464075a3ebaea43ce Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 18 Sep 2021 16:48:27 +0200 Subject: [PATCH 2/4] fix bug --- pandas/io/formats/style_render.py | 44 +++++++++++++++---------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 48416b2a2db45..451c6f2fca923 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -620,32 +620,30 @@ def _translate_latex(self, d: dict) -> None: for r, row in enumerate(d["head"]) ] body = [] - for r, row in enumerate(d["body"]): - 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] - if col["is_visible"] - else [], - } - for c, col in enumerate(row) - if col["type"] == "th" - ] - - row_body_cells = [ - {**col, "cellstyle": self.ctx[r, c - self.data.index.nlevels]} + rows = [row for r, row in enumerate(d["body"]) if r not in self.hidden_rows] + for r, row in enumerate(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] if col["is_visible"] else [], + } for c, col in enumerate(row) - if (col["is_visible"] and col["type"] == "td") + if col["type"] == "th" ] - body.append(row_body_headers + row_body_cells) + 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) d["body"] = body def format( From 8e17483740cd5c92f6bf8a30d87d1ae61dab98aa Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 18 Sep 2021 16:51:17 +0200 Subject: [PATCH 3/4] fix bug --- pandas/tests/io/formats/style/test_to_latex.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/io/formats/style/test_to_latex.py b/pandas/tests/io/formats/style/test_to_latex.py index 913317e129bf9..f252a89179c12 100644 --- a/pandas/tests/io/formats/style/test_to_latex.py +++ b/pandas/tests/io/formats/style/test_to_latex.py @@ -785,6 +785,7 @@ def test_siunitx_basic_headers(styler): def test_hide_index_latex(styler): + # GH 43637 styler.hide_index([0]) result = styler.to_latex() expected = dedent( From 22899e4b1d296d04ad29a762724b1727107b0264 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Sat, 18 Sep 2021 16:57:46 +0200 Subject: [PATCH 4/4] whats new --- doc/source/whatsnew/v1.3.4.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.3.4.rst b/doc/source/whatsnew/v1.3.4.rst index daefa9dc618f5..726ea426e9e08 100644 --- a/doc/source/whatsnew/v1.3.4.rst +++ b/doc/source/whatsnew/v1.3.4.rst @@ -28,6 +28,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ - Fixed bug in :meth:`.GroupBy.mean` with datetimelike values including ``NaT`` values returning incorrect results (:issue`:43132`) +- Fixed bug in :meth:`.Styler.to_latex` where hidden rows were not respected (:issue:`43637`) .. ---------------------------------------------------------------------------