Skip to content

Commit 96d51a7

Browse files
corbanvillaattack68mroeschke
authored andcommitted
Fix Bug: last column missing formatting in Latex (pandas-dev#52218) (pandas-dev#60356)
* fix to_latex for multiindex * add newline * fix styling * Update v2.3.0.rst fix backticks * update description. * Apply suggested fix Co-authored-by: JHM Darbyshire <[email protected]> * move to v.3 * Update pandas/io/formats/style_render.py Co-authored-by: Matthew Roeschke <[email protected]> * spacing * fix whitespace * fix method name --------- Co-authored-by: JHM Darbyshire <[email protected]> Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 93fbe50 commit 96d51a7

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

doc/source/whatsnew/v3.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ ExtensionArray
764764

765765
Styler
766766
^^^^^^
767-
-
767+
- Bug in :meth:`Styler.to_latex` where styling column headers when combined with a hidden index or hidden index-levels is fixed.
768768

769769
Other
770770
^^^^^

pandas/io/formats/style_render.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,8 @@ def _translate_latex(self, d: dict, clines: str | None) -> None:
868868
or multirow sparsification (so that \multirow and \multicol work correctly).
869869
"""
870870
index_levels = self.index.nlevels
871-
visible_index_level_n = index_levels - sum(self.hide_index_)
871+
# GH 52218
872+
visible_index_level_n = max(1, index_levels - sum(self.hide_index_))
872873
d["head"] = [
873874
[
874875
{**col, "cellstyle": self.ctx_columns[r, c - visible_index_level_n]}

pandas/tests/io/formats/test_to_latex.py

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

0 commit comments

Comments
 (0)