Skip to content

BUG: DataFrame.style.to_latex() does not allow index=False as a parameter and does not return consistent #55188

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
xhluca opened this issue Sep 18, 2023 · 5 comments
Labels
Closing Candidate May be closeable, needs more eyeballs Styler conditional formatting using DataFrame.style Usage Question

Comments

@xhluca
Copy link

xhluca commented Sep 18, 2023

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 numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))
# Will have a Future warning
print(df.to_latex(index=False))

# This will not work:
print(df.style.to_latex(index=False))

Issue Description

The second will not work, but it would be great to keep the option for index=False in order to ensure backward compatibility (unless the futurewarning applies to pandas 3.0+)

I expect this will break backward compatibility if pandas is upgraded to <3 but >2.1 (according to futurewarning)

Expected Behavior

df.style.to_latex(index=False) should work just like df.style.hide_index().to_latex()

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.8.16.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-56-generic
Version : #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.3
numpy : 1.24.4
pytz : 2023.3
dateutil : 2.8.2
setuptools : 56.0.0
pip : 22.0.4
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.12.2
pandas_datareader: None
bs4 : 4.11.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2023.6.0
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 13.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@xhluca xhluca added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 18, 2023
@paulreece
Copy link
Contributor

paulreece commented Sep 20, 2023

I do get the TypeError for print(df.style.to_latex(index=False)) on the main development branch. A maintainer will need to chime in in regards to whether they want to implement this or not.

>>> print(df.style.to_latex(index=False))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Styler.to_latex() got an unexpected keyword argument 'index'

The Future warning doesn't occur for print(df.to_latex(index=False)) on the main development branch

>>> print(df.to_latex(index=False))
\begin{tabular}{rrrr}
\toprule
A & B & C & D \\
\midrule
-1.141720 & -0.864452 & 0.281404 & -0.191118 \\
0.339318 & 0.526344 & -0.273565 & -1.143412 \\
-0.097656 & -0.653314 & -1.001223 & 0.227216 \\
0.916563 & -0.667892 & 0.759713 & 0.856244 \\
-2.211459 & -0.143264 & -0.364037 & -0.379071 \\
-0.658746 & 0.961309 & 1.973678 & -0.464108 \\
-0.874616 & 0.039770 & -0.398601 & -0.333741 \\
-0.568972 & 0.631231 & -1.278331 & 0.050330 \\
0.803613 & 0.155475 & -0.370566 & -1.728852 \\
-0.597297 & -1.037614 & -1.215433 & 1.180112 \\
\bottomrule
\end{tabular}

@rhshadrach
Copy link
Member

cc @attack68

@rhshadrach rhshadrach added the Styler conditional formatting using DataFrame.style label Sep 20, 2023
@attack68
Copy link
Contributor

This will not be allowed:

df.style.to_latex(index=False)

The solution is:

df.style.hide().to_latex()

The reason is to minimise the arguments supplied to to_latex whilst providing a very flexible display control system. You see the the answer to the index=? parameter is not a boolean, it is:

  • Is the whole index displayed or hidden?
  • Is a sub level of the multi-index shown or hidden?
  • which index is it, rows or columns?
  • is it the index itself to be hidden or do rows or columns need to be hidden?

I understand that index=False is provided in DataFrame.to_latex(). This is for backwards capability with that function. This is syntactic sugar and is also not very good design but unfortunately it has a legacy of being there.

@attack68 attack68 added Closing Candidate May be closeable, needs more eyeballs Usage Question and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 21, 2023
@xhluca
Copy link
Author

xhluca commented Sep 21, 2023

Then should the futurewarning be removed, since there's no plan to remove index=False until v3?

@attack68
Copy link
Contributor

You have ticked "I have confirmed this exists on the latest version of pandas". You have specified you are using 1.5.3. The latest version is 2.1.x. @paulreece states:

The Future warning doesn't occur for print(df.to_latex(index=False)) on the main development branch

Feel free to close if this addresses your query.

@xhluca xhluca closed this as completed Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closing Candidate May be closeable, needs more eyeballs Styler conditional formatting using DataFrame.style Usage Question
Projects
None yet
Development

No branches or pull requests

4 participants