Skip to content

Commit 8988e6c

Browse files
committed
fix to_latex for multiindex
1 parent 7fe270c commit 8988e6c

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

doc/source/whatsnew/v2.3.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ These are bug fixes that might have notable behavior changes.
4848

4949
.. _whatsnew_230.notable_bug_fixes.notable_bug_fix1:
5050

51+
- The `to_latex` is now fixed for hidden multiindex dataframes (:issue:`52218`)
52+
5153
notable_bug_fix1
5254
^^^^^^^^^^^^^^^^
5355

pandas/io/formats/style_render.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,8 @@ def _translate_latex(self, d: dict, clines: str | None) -> None:
869869
"""
870870
index_levels = self.index.nlevels
871871
visible_index_level_n = index_levels - sum(self.hide_index_)
872+
if visible_index_level_n == 0:
873+
visible_index_level_n = 1
872874
d["head"] = [
873875
[
874876
{**col, "cellstyle": self.ctx_columns[r, c - visible_index_level_n]}

pandas/tests/io/formats/test_to_latex.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,3 +1405,85 @@ def test_to_latex_multiindex_multirow(self):
14051405
"""
14061406
)
14071407
assert result == expected
1408+
1409+
def test_to_latex_multiindex_format_single_index_hidden(self):
1410+
# GH 52218
1411+
df = pd.DataFrame({
1412+
"A": [1, 2],
1413+
"B": [4, 5],
1414+
})
1415+
result = (
1416+
df.style.hide(axis="index")
1417+
.map_index(lambda v: "textbf:--rwrap;", axis="columns")
1418+
.to_latex()
1419+
)
1420+
expected = _dedent(r"""
1421+
\begin{tabular}{rr}
1422+
\textbf{A} & \textbf{B} \\
1423+
1 & 4 \\
1424+
2 & 5 \\
1425+
\end{tabular}
1426+
"""
1427+
)
1428+
assert result == expected
1429+
1430+
def test_to_latex_multiindex_format_triple_index_two_hidden(self):
1431+
# GH 52218
1432+
arrays = [
1433+
["A", "A", "B", "B"],
1434+
["one", "two", "one", "two"],
1435+
["x", "x", "y", "y"],
1436+
]
1437+
index = pd.MultiIndex.from_arrays(arrays, names=["Level 0", "Level 1", "Level 2"])
1438+
df = pd.DataFrame(
1439+
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
1440+
index=index,
1441+
columns=["C1", "C2", "C3"]
1442+
)
1443+
result = (
1444+
df.style.hide(axis="index", level=[0, 1])
1445+
.map_index(lambda v: "textbf:--rwrap;", axis="columns")
1446+
.to_latex()
1447+
)
1448+
expected = _dedent(r"""
1449+
\begin{tabular}{lrrr}
1450+
& \textbf{C1} & \textbf{C2} & \textbf{C3} \\
1451+
Level 2 & & & \\
1452+
x & 0 & 0 & 0 \\
1453+
x & 0 & 0 & 0 \\
1454+
y & 0 & 0 & 0 \\
1455+
y & 0 & 0 & 0 \\
1456+
\end{tabular}
1457+
"""
1458+
)
1459+
assert result == expected
1460+
1461+
def test_to_latex_multiindex_format_triple_index_all_hidden(self):
1462+
# GH 52218
1463+
arrays = [
1464+
["A", "A", "B", "B"],
1465+
["one", "two", "one", "two"],
1466+
["x", "x", "y", "y"],
1467+
]
1468+
index = pd.MultiIndex.from_arrays(arrays, names=["Level 0", "Level 1", "Level 2"])
1469+
df = pd.DataFrame(
1470+
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
1471+
index=index,
1472+
columns=["C1", "C2", "C3"]
1473+
)
1474+
result = (
1475+
df.style.hide(axis="index", level=[0, 1, 2])
1476+
.map_index(lambda v: "textbf:--rwrap;", axis="columns")
1477+
.to_latex()
1478+
)
1479+
expected = _dedent(r"""
1480+
\begin{tabular}{rrr}
1481+
\textbf{C1} & \textbf{C2} & \textbf{C3} \\
1482+
0 & 0 & 0 \\
1483+
0 & 0 & 0 \\
1484+
0 & 0 & 0 \\
1485+
0 & 0 & 0 \\
1486+
\end{tabular}
1487+
"""
1488+
)
1489+
assert result == expected

0 commit comments

Comments
 (0)