Skip to content

BUG: to_latex compute wrong multicolumn size in longtable environment with column_format set #46037

Closed
@tgrandje

Description

@tgrandje

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame([[1, 2, 3], [4,5,6]], columns=['A', 'B', 'C'])
print(df.style.to_latex(environment="longtable", column_format="r{10cm}rr"))

# Other styling possibility :
# print(df.style.to_latex(environment="longtable", column_format="r >{\columncolor{Gray}}r r"))

Issue Description

The multicolumn count derived from the 'column_format' argument seems to be evaluated as the length of the passed string. This has an impact used in the longtable environment only, as it displays a \multicolumn{X}{r}{Continued on next page} \\.

If you pass column_format="r{10cm}rr", you will end up with \multicolumn{9}{r}{Continued on next page} \\ which will trigger an error in LaTeX (9 == len(r{10cm}rr").

Just to be clear, the complete result of the first print in the example is :

\begin{longtable}{r{10cm}rr}
 & A & B & C \\
\endfirsthead
 & A & B & C \\
\endhead
\multicolumn{9}{r}{Continued on next page} \\  % <-- this will trigger an error in LaTeX
\endfoot
\endlastfoot
0 & 1 & 2 & 3 \\
1 & 4 & 5 & 6 \\
\end{longtable}

Expected Behavior

The column count should probably be deduced from the dataframe's shape (and the index's shape if included in the to_latex), without trying to decode the column_format string (?).

Installed Versions

NSTALLED VERSIONS

commit : bb1f651
python : 3.9.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-87-generic
Version : #98~18.04.1-Ubuntu SMP Wed Sep 22 10:45:04 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : fr_FR.UTF-8
LOCALE : fr_FR.UTF-8

pandas : 1.4.0
numpy : 1.22.1
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 60.2.0
Cython : None
pytest : 5.4.3
hypothesis : None
sphinx : 4.4.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.7.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 7.31.1
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
fastparquet : None
fsspec : 2022.01.0
gcsfs : None
matplotlib : 3.5.1
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.6.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugStylerconditional formatting using DataFrame.style

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions