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

ENH: Adding \clines to LaTeX table if index is not output #59877

Open
3 tasks done
edbennett opened this issue Sep 23, 2024 · 3 comments
Open
3 tasks done

ENH: Adding \clines to LaTeX table if index is not output #59877

edbennett opened this issue Sep 23, 2024 · 3 comments

Comments

@edbennett
Copy link

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": 1, "b": 2, "c": 3},
        {"a": 1, "b": 3, "c": 5},
        {"a": 2, "b": 9, "c": 12},
        {"a": 3, "b": 2, "c": 123},
    ]
)
print(
    df.set_index("a").style.hide(axis=0).to_latex(clines="all;data", hrules=True)
)

Issue Description

I am trying to output a LaTeX table with groups separated by \clines but where the grouping variable is not shown in the table. I had thought that having the grouping in the index, but hiding the index in the output table, would allow this to happen; however, instead specifying clines="all;data" does nothing when the index is hidden.

Expected Behavior

I would expect the example above to output

\begin{tabular}{rr}
\toprule
b & c \\
\midrule
2 & 3 \\
\cline{1-2}
3 & 5 \\
\cline{1-2}
9 & 12 \\
\cline{1-2}
2 & 123 \\
\bottomrule
\end{tabular}

Currently it outputs

\begin{tabular}{rr}
\toprule
b & c \\
\midrule
2 & 3 \\
3 & 5 \\
9 & 12 \\
2 & 123 \\
\bottomrule
\end{tabular}

(Ideally I would want it to output

\begin{tabular}{rr}
\toprule
b & c \\
\midrule
2 & 3 \\
3 & 5 \\
\cline{1-2}
9 & 12 \\
\cline{1-2}
2 & 123 \\
\bottomrule
\end{tabular}

but there is a problem with the way that I am creating the index, which is unrelated to the issue at hand.)

Installed Versions

INSTALLED VERSIONS

commit : 71b395f
python : 3.12.6
python-bits : 64
OS : Darwin
OS-release : 23.6.0
Version : Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:30 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 3.0.0.dev0+1497.g71b395f2cf
numpy : 2.1.1
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : 8.27.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.4
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pytz : 2024.2
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@edbennett edbennett added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 23, 2024
@rhshadrach
Copy link
Member

Thanks for the the report. The argument is documented as:

Use to control adding \cline commands for the index labels separation.

As such, I do not believe it is a bug when you have no index labels. I'm reworking this issue as a feature request. At a glance, it seems to make sense to have cline between rows whether the index is present or not.

cc @attack68 for any thoughts.

@rhshadrach rhshadrach added Enhancement IO LaTeX to_latex Needs Discussion Requires discussion from core team before further action and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 24, 2024
@rhshadrach rhshadrach changed the title BUG: Adding \clines to LaTeX table doesn't work if index is not output ENH: Adding \clines to LaTeX table if index is not output Sep 24, 2024
@attack68
Copy link
Contributor

I would agree. If this isnt possible already it probably should be if the index is hidden. From memory this was quite complicated though when accounting for (and testing) different cases with hidden indexes and or hidden columns.

@rhshadrach rhshadrach removed the Needs Discussion Requires discussion from core team before further action label Sep 27, 2024
@rhshadrach
Copy link
Member

Thanks @attack68 - I think PRs to implement this are welcome!

edbennett added a commit to edbennett/pandas that referenced this issue Sep 28, 2024
…as-dev#59877]

- implement a new suffix for the `clines` option, `-invisible`, doubling the number of available options, specifying that hidden indices should be included when deciding whether to add \clines
- add tests for this behaviour
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants