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`) .. --------------------------------------------------------------------------- diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 0ec6a9b470b50..451c6f2fca923 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -620,7 +620,8 @@ def _translate_latex(self, d: dict) -> None: for r, row in enumerate(d["head"]) ] body = [] - for r, row in enumerate(d["body"]): + 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: diff --git a/pandas/tests/io/formats/style/test_to_latex.py b/pandas/tests/io/formats/style/test_to_latex.py index 40ba3ca26afa4..f252a89179c12 100644 --- a/pandas/tests/io/formats/style/test_to_latex.py +++ b/pandas/tests/io/formats/style/test_to_latex.py @@ -782,3 +782,18 @@ 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): + # GH 43637 + 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