Skip to content
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ These are bug fixes that might have notable behavior changes.

.. _whatsnew_230.notable_bug_fixes.notable_bug_fix1:

- The ``to_latex`` function is now fixed for hidden multiindex dataframes (:issue:`52218`)
Copy link
Contributor

@attack68 attack68 Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is the right place for this, and its a bit misleading as well. I would write.

...to_latex styling of column headers when combined with a hidden index or hidden index-levels is fixed...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@attack68 Ah didn't see the general bug fixes section... moved and reworded 👍


notable_bug_fix1
^^^^^^^^^^^^^^^^

Expand Down
2 changes: 2 additions & 0 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,8 @@ def _translate_latex(self, d: dict, clines: str | None) -> None:
"""
index_levels = self.index.nlevels
visible_index_level_n = index_levels - sum(self.hide_index_)
if visible_index_level_n == 0:
visible_index_level_n = 1
d["head"] = [
[
{**col, "cellstyle": self.ctx_columns[r, c - visible_index_level_n]}
Expand Down
85 changes: 85 additions & 0 deletions pandas/tests/io/formats/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1405,3 +1405,88 @@ def test_to_latex_multiindex_multirow(self):
"""
)
assert result == expected

def test_to_latex_multiindex_format_single_index_hidden(self):
# GH 52218
df = DataFrame(
{
"A": [1, 2],
"B": [4, 5],
}
)
result = (
df.style.hide(axis="index")
.map_index(lambda v: "textbf:--rwrap;", axis="columns")
.to_latex()
)
expected = _dedent(r"""
\begin{tabular}{rr}
\textbf{A} & \textbf{B} \\
1 & 4 \\
2 & 5 \\
\end{tabular}
""")
assert result == expected

def test_to_latex_multiindex_format_triple_index_two_hidden(self):
# GH 52218
arrays = [
["A", "A", "B", "B"],
["one", "two", "one", "two"],
["x", "x", "y", "y"],
]
index = pd.MultiIndex.from_arrays(
arrays, names=["Level 0", "Level 1", "Level 2"]
)
df = DataFrame(
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
index=index,
columns=["C1", "C2", "C3"],
)
result = (
df.style.hide(axis="index", level=[0, 1])
.map_index(lambda v: "textbf:--rwrap;", axis="columns")
.to_latex()
)
expected = _dedent(r"""
\begin{tabular}{lrrr}
& \textbf{C1} & \textbf{C2} & \textbf{C3} \\
Level 2 & & & \\
x & 0 & 0 & 0 \\
x & 0 & 0 & 0 \\
y & 0 & 0 & 0 \\
y & 0 & 0 & 0 \\
\end{tabular}
""")
assert result == expected

def test_to_latex_multiindex_format_triple_index_all_hidden(self):
# GH 52218
arrays = [
["A", "A", "B", "B"],
["one", "two", "one", "two"],
["x", "x", "y", "y"],
]
index = pd.MultiIndex.from_arrays(
arrays, names=["Level 0", "Level 1", "Level 2"]
)
df = DataFrame(
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
index=index,
columns=["C1", "C2", "C3"],
)
result = (
df.style.hide(axis="index", level=[0, 1, 2])
.map_index(lambda v: "textbf:--rwrap;", axis="columns")
.to_latex()
)
expected = _dedent(r"""
\begin{tabular}{rrr}
\textbf{C1} & \textbf{C2} & \textbf{C3} \\
0 & 0 & 0 \\
0 & 0 & 0 \\
0 & 0 & 0 \\
0 & 0 & 0 \\
\end{tabular}
""")
assert result == expected
Loading