Skip to content

backport 43638: BUG: styler to_latex doesnt respect hide_index #43640

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

.. ---------------------------------------------------------------------------

Expand Down
3 changes: 2 additions & 1 deletion pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/io/formats/style/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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