Skip to content

Commit e909ea0

Browse files
syxolkjreback
authored andcommitted
BUG: Fix to_latex with longtable (#17959) (#17960)
closes #17959
1 parent 3821040 commit e909ea0

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

doc/source/whatsnew/v0.21.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ I/O
127127
- Bug in :func:`pandas.io.json.json_normalize` to avoid modification of ``meta`` (:issue:`18610`)
128128
- Bug in :func:`to_latex` where repeated multi-index values were not printed even though a higher level index differed from the previous row (:issue:`14484`)
129129
- Bug when reading NaN-only categorical columns in :class:`HDFStore` (:issue:`18413`)
130+
- Bug in :meth:`DataFrame.to_latex` with ``longtable=True`` where a latex multicolumn always spanned over three columns (:issue:`17959`)
130131

131132
Plotting
132133
^^^^^^^^

pandas/io/formats/format.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -962,8 +962,8 @@ def get_col_type(dtype):
962962
if self.longtable:
963963
buf.write('\\endhead\n')
964964
buf.write('\\midrule\n')
965-
buf.write('\\multicolumn{3}{r}{{Continued on next '
966-
'page}} \\\\\n')
965+
buf.write('\\multicolumn{{{n}}}{{r}}{{{{Continued on next '
966+
'page}}}} \\\\\n'.format(n=len(row)))
967967
buf.write('\\midrule\n')
968968
buf.write('\\endfoot\n\n')
969969
buf.write('\\bottomrule\n')

pandas/tests/io/formats/test_to_latex.py

+33-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ def test_to_latex_format(self, frame):
9191

9292
assert withindex_result == withindex_expected
9393

94+
def test_to_latex_empty(self):
95+
df = DataFrame()
96+
result = df.to_latex()
97+
expected = r"""\begin{tabular}{l}
98+
\toprule
99+
Empty DataFrame
100+
Columns: Index([], dtype='object')
101+
Index: Index([], dtype='object') \\
102+
\bottomrule
103+
\end{tabular}
104+
"""
105+
assert result == expected
106+
107+
result = df.to_latex(longtable=True)
108+
expected = r"""\begin{longtable}{l}
109+
\toprule
110+
Empty DataFrame
111+
Columns: Index([], dtype='object')
112+
Index: Index([], dtype='object') \\
113+
\end{longtable}
114+
"""
115+
assert result == expected
116+
94117
def test_to_latex_with_formatters(self):
95118
df = DataFrame({'int': [1, 2, 3],
96119
'float': [1.0, 2.0, 3.0],
@@ -377,7 +400,7 @@ def test_to_latex_longtable(self, frame):
377400
1 & 2 & b2 \\
378401
\end{longtable}
379402
"""
380-
403+
open("expected.txt", "w").write(withindex_result)
381404
assert withindex_result == withindex_expected
382405

383406
withoutindex_result = df.to_latex(index=False, longtable=True)
@@ -387,7 +410,7 @@ def test_to_latex_longtable(self, frame):
387410
\midrule
388411
\endhead
389412
\midrule
390-
\multicolumn{3}{r}{{Continued on next page}} \\
413+
\multicolumn{2}{r}{{Continued on next page}} \\
391414
\midrule
392415
\endfoot
393416
@@ -400,6 +423,14 @@ def test_to_latex_longtable(self, frame):
400423

401424
assert withoutindex_result == withoutindex_expected
402425

426+
df = DataFrame({'a': [1, 2]})
427+
with1column_result = df.to_latex(index=False, longtable=True)
428+
assert "\multicolumn{1}" in with1column_result
429+
430+
df = DataFrame({'a': [1, 2], 'b': [3, 4], 'c': [5, 6]})
431+
with3columns_result = df.to_latex(index=False, longtable=True)
432+
assert "\multicolumn{3}" in with3columns_result
433+
403434
def test_to_latex_escape_special_chars(self):
404435
special_characters = ['&', '%', '$', '#', '_', '{', '}', '~', '^',
405436
'\\']

0 commit comments

Comments
 (0)