Skip to content

BUG: hrules and clines in .to_latex() draw two lines at bottom of table  #49723

@stevenlis

Description

@stevenlis

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({'A': ['a', 'a', 'b'], 'B': ['a', 'b', 'a']})
df = df.groupby(['A', 'B']).size().to_frame()

print(
    df.style.to_latex(
        hrules=True,
        clines='all;data'
    )
)

Issue Description

The code above produces the following latex code (I added latex preamble).

\documentclass[12pt,letterpaper]{article}
\usepackage{booktabs}
\usepackage{multirow}

\begin{document}

\begin{tabular}{llr}
    \toprule
     &  & 0 \\
    A & B &  \\
    \midrule
    \multirow[c]{2}{*}{a} & a & 1 \\
    \cline{2-3}
     & b & 1 \\
    \cline{1-3} \cline{2-3}
    b & a & 1 \\
    \cline{1-3} \cline{2-3}
    \bottomrule
\end{tabular}

\end{document}

which gives
CS-X227SZFy

You can see that there are two lines at the bottom of the table clines and \bottomrule.

Expected Behavior

In this example, to make the table more readable, it is preferred to use both hrules and clines. I don't know if there is something you can add in the latex preamble. One idea might be just let hrules to accept a list so that a user could pass ['toprule', 'midrule'] to avoid the bottomrule.

Installed Versions

INSTALLED VERSIONS

commit : 91111fd
python : 3.9.12.final.0
python-bits : 64
OS : Darwin
OS-release : 22.1.0
Version : Darwin Kernel Version 22.1.0: Sun Oct 9 20:15:52 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8112
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.5.1
numpy : 1.23.1
pytz : 2022.5
dateutil : 2.8.2
setuptools : 65.5.0
pip : 22.3
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.5.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : 1.3.5
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.1
numba : None
numexpr : 2.8.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.3
snappy : None
sqlalchemy : None
tables : None
tabulate : 0.8.10
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None
/opt/miniconda3/envs/desktop/lib/python3.9/site-packages/_distutils_hack/init.py:33: UserWarning: Setuptools is replacing distutils.
warnings.warn("Setuptools is replacing distutils.")

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions