diff --git a/doc/source/whatsnew/v1.3.4.rst b/doc/source/whatsnew/v1.3.4.rst index e56e481355ac2..708609e956da4 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 a2841ebcbab02..38f75a3f8abb1 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -539,7 +539,8 @@ def _translate_latex(self, d: dict) -> None: """ d["head"] = [[col for col in row if col["is_visible"]] for row in 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 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 55b17dc37adda..e9db464adf0c8 100644 --- a/pandas/tests/io/formats/style/test_to_latex.py +++ b/pandas/tests/io/formats/style/test_to_latex.py @@ -505,3 +505,18 @@ def test_styler_object_after_render(styler): assert pre_render.table_styles == styler.table_styles assert pre_render.caption == styler.caption + + +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