Skip to content

Commit 924c419

Browse files
loicseguinjreback
authored andcommitted
BUG: remove midrule in latex output with header=False
1 parent aa94ae4 commit 924c419

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

doc/source/whatsnew/v0.17.1.txt

+25
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,29 @@ Bug Fixes
4444
~~~~~~~~~
4545

4646
- Bug in ``.to_latex()`` output broken when the index has a name (:issue: `10660`)
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
4759
- Bug in ``squeeze()`` with zero length arrays (:issue:`11230`, :issue:`8999`)
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
- Bug in ``DataFrame.to_latex()`` produces an extra rule when ``header=False`` (:issue:`7124`)

pandas/core/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ def write(buf, frame, column_format, strcols, longtable=False):
672672
if any(frame.index.names):
673673
nlevels += 1
674674
for i, row in enumerate(zip(*strcols)):
675-
if i == nlevels:
675+
if i == nlevels and self.header:
676676
buf.write('\\midrule\n') # End of header
677677
if longtable:
678678
buf.write('\\endhead\n')

pandas/tests/test_format.py

+24
Original file line numberDiff line numberDiff line change
@@ -2793,6 +2793,30 @@ def test_to_latex_escape_special_chars(self):
27932793
"""
27942794
self.assertEqual(observed, expected)
27952795

2796+
def test_to_latex_no_header(self):
2797+
# GH 7124
2798+
df = DataFrame({'a': [1, 2],
2799+
'b': ['b1', 'b2']})
2800+
withindex_result = df.to_latex(header=False)
2801+
withindex_expected = r"""\begin{tabular}{lrl}
2802+
\toprule
2803+
0 & 1 & b1 \\
2804+
1 & 2 & b2 \\
2805+
\bottomrule
2806+
\end{tabular}
2807+
"""
2808+
self.assertEqual(withindex_result, withindex_expected)
2809+
2810+
withoutindex_result = df.to_latex(index=False, header=False)
2811+
withoutindex_expected = r"""\begin{tabular}{rl}
2812+
\toprule
2813+
1 & b1 \\
2814+
2 & b2 \\
2815+
\bottomrule
2816+
\end{tabular}
2817+
"""
2818+
self.assertEqual(withoutindex_result, withoutindex_expected)
2819+
27962820
def test_to_csv_quotechar(self):
27972821
df = DataFrame({'col' : [1,2]})
27982822
expected = """\

0 commit comments

Comments
 (0)