Skip to content

Commit 38c7b62

Browse files
committed
Added tests for to_latex
1 parent c1f34c2 commit 38c7b62

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

pandas/tests/formats/test_to_latex.py

+45
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,51 @@ def test_to_latex_no_header(self):
428428

429429
assert withoutindex_result == withoutindex_expected
430430

431+
def test_to_latex_specified_header(self):
432+
# GH 7124
433+
df = DataFrame({'a': [1, 2], 'b': ['b1', 'b2']})
434+
withindex_result = df.to_latex(header=['AA', 'BB'])
435+
withindex_expected = r"""\begin{tabular}{lrl}
436+
\toprule
437+
{} & AA & BB \\
438+
\midrule
439+
0 & 1 & b1 \\
440+
1 & 2 & b2 \\
441+
\bottomrule
442+
\end{tabular}
443+
"""
444+
445+
assert withindex_result == withindex_expected
446+
447+
withoutindex_result = df.to_latex(header=['AA', 'BB'], index=False)
448+
withoutindex_expected = r"""\begin{tabular}{rl}
449+
\toprule
450+
AA & BB \\
451+
\midrule
452+
1 & b1 \\
453+
2 & b2 \\
454+
\bottomrule
455+
\end{tabular}
456+
"""
457+
458+
assert withoutindex_result == withoutindex_expected
459+
460+
withoutescape_result = df.to_latex(header=['$A$', '$B$'], escape=False)
461+
withoutescape_expected = r"""\begin{tabular}{lrl}
462+
\toprule
463+
{} & $A$ & $B$ \\
464+
\midrule
465+
0 & 1 & b1 \\
466+
1 & 2 & b2 \\
467+
\bottomrule
468+
\end{tabular}
469+
"""
470+
471+
assert withoutescape_result == withoutescape_expected
472+
473+
with tm.assertRaises(ValueError):
474+
df.to_latex(header=['A'])
475+
431476
def test_to_latex_decimal(self, frame):
432477
# GH 12031
433478
frame.to_latex()

0 commit comments

Comments
 (0)