Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 of 3 tasks
tgrandje opened this issue Feb 17, 2022 · 4 comments · Fixed by #46057
Closed
2 of 3 tasks
Labels
Bug Styler conditional formatting using DataFrame.style
Milestone

Comments

@tgrandje
Copy link

tgrandje commented Feb 17, 2022

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

@tgrandje tgrandje added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 17, 2022
@tgrandje
Copy link
Author

Seems to be coming from that line of code

@eshirvana
Copy link
Contributor

@tgrandje what's the best solution, I can work on this bug

@tgrandje
Copy link
Author

tgrandje commented Feb 17, 2022

@eshirvana Not really sure, I haven't understood where the shape of the latex table is computed. When I try to compute the test from LongTableBuilder I can't reproduce this bug...

@phofl
Copy link
Member

phofl commented Feb 18, 2022

Could you give a descriptive title?

cc @attack68

@tgrandje tgrandje changed the title BUG: BUG: to_latex compute wrong multicolumn in longtable environment with column_format set Feb 18, 2022
@tgrandje tgrandje changed the title BUG: to_latex compute wrong multicolumn in longtable environment with column_format set BUG: to_latex compute wrong multicolumn size in longtable environment with column_format set Feb 18, 2022
@phofl phofl added the Styler conditional formatting using DataFrame.style label Feb 18, 2022
@jreback jreback added this to the 1.4.2 milestone Feb 26, 2022
@jreback jreback removed the Needs Triage Issue that has not been reviewed by a pandas team member label Feb 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants