Skip to content

Commit 7a71573

Browse files
committed
BUG: Fix to_latex with longtable (#17959)
pandas.DataFrame.to_latex(longtable=True) always contained \multicolumn{3}{r}{{Continued on next page}} regardless of the number of columns in the output.
1 parent e1dabf3 commit 7a71573

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,7 @@ I/O
998998
- Bug in :meth:`DataFrame.to_html` in which there was no validation of the ``justify`` parameter (:issue:`17527`)
999999
- Bug in :func:`HDFStore.select` when reading a contiguous mixed-data table featuring VLArray (:issue:`17021`)
10001000
- Bug in :func:`to_json` where several conditions (including objects with unprintable symbols, objects with deep recursion, overlong labels) caused segfaults instead of raising the appropriate exception (:issue:`14256`)
1001+
- Bug in :meth:`DataFrame.to_latex` with ``longtable=True`` where a latex multicolumn always spanned over three columns (:issue:`17959`)
10011002

10021003
Plotting
10031004
^^^^^^^^

pandas/io/formats/format.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,8 @@ def get_col_type(dtype):
942942
if self.longtable:
943943
buf.write('\\endhead\n')
944944
buf.write('\\midrule\n')
945-
buf.write('\\multicolumn{3}{r}{{Continued on next '
946-
'page}} \\\\\n')
945+
buf.write('\\multicolumn{{{n}}}{{r}}{{{{Continued on next '
946+
'page}}}} \\\\\n'.format(n=len(row)))
947947
buf.write('\\midrule\n')
948948
buf.write('\\endfoot\n\n')
949949
buf.write('\\bottomrule\n')

pandas/tests/io/formats/test_to_latex.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def test_to_latex_longtable(self, frame):
365365
\midrule
366366
\endhead
367367
\midrule
368-
\multicolumn{3}{r}{{Continued on next page}} \\
368+
\multicolumn{2}{r}{{Continued on next page}} \\
369369
\midrule
370370
\endfoot
371371

0 commit comments

Comments
 (0)