Skip to content
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

BUG: multicolumn of to_latex does not work correctly when index==False #43924

Closed
2 of 3 tasks
Alalalalaki opened this issue Oct 8, 2021 · 2 comments
Closed
2 of 3 tasks
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@Alalalalaki
Copy link

Alalalalaki commented Oct 8, 2021

  • 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 master branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame([[1,2,3,4]], columns=pd.MultiIndex.from_tuples([('1', 'a'), ('1', 'b'), ('2', 'a'), ('2', 'b'), ]))
print(df.to_latex(index=False, multicolumn=True,))

\begin{tabular}{rrrr}
\toprule
1 & \multicolumn{2}{l}{2} \\
a & b & a & b \\
\midrule
1 & 2 & 3 & 4 \\
\bottomrule
\end{tabular}

Issue Description

The top level of multicolumns are not located correctly.

Expected Behavior

\begin{tabular}{rrrr}
\toprule
\multicolumn{2}{l}{1} & \multicolumn{2}{l}{2} \
a & b & a & b \
\midrule
1 & 2 & 3 & 4 \
\bottomrule
\end{tabular}

Installed Versions

INSTALLED VERSIONS

commit : c7f7443
python : 3.8.10.final.0
python-bits : 64
OS : Darwin
OS-release : 20.6.0
Version : Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.1
numpy : 1.21.1
pytz : 2021.1
dateutil : 2.8.2
pip : 21.2.2
setuptools : 57.4.0
Cython : 0.29.24
pytest : 6.2.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.25.0
pandas_datareader: 0.10.0
bs4 : 4.9.3
bottleneck : None
fsspec : 2021.07.0
fastparquet : None
gcsfs : None
matplotlib : 3.4.2
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : None
scipy : 1.7.0
sqlalchemy : None
tables : 3.6.1
tabulate : 0.8.9
xarray : 0.19.0
xlrd : 2.0.1
xlwt : None
numba : 0.53.1

@Alalalalaki Alalalalaki added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 8, 2021
@attack68
Copy link
Contributor

attack68 commented Oct 9, 2021

Im working to phase out the pandas.LaTeXFormatter class and replace it with the Styler.template_latex jinja2 renderer instead, but this hasn't been implemented yet. (#41649)

In the meantime you can do this:

import pandas as pd
df = pd.DataFrame([[1,2,3,4]], columns=pd.MultiIndex.from_tuples([('1', 'a'), ('1', 'b'), ('2', 'a'), ('2', 'b'), ]))
styler = df.style.hide_index()
print(styler.to_latex(hrules=True, multicol_align="l")

\begin{tabular}{rrrr}
\toprule
\multicolumn{2}{l}{1} & \multicolumn{2}{l}{2} \\
a & b & a & b \\
\midrule
1 & 2 & 3 & 4 \\
\bottomrule
\end{tabular}

Please close if this address your issue.

@Alalalalaki
Copy link
Author

Close as being solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants