pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.77k stars 17.97k forks source link

BUG: to_latex() does not escape index name #57362

Open bileamScheuvens opened 9 months ago

bileamScheuvens commented 9 months ago

Pandas version checks

Reproducible Example

import pandas as pd

pd.DataFrame({'_A': [1], '_B': ['a']}).set_index("_A").to_latex(escape=True)
# produces:
'\\begin{tabular}{ll}\n\\toprule\n & \\_B \\\\\n_A &  \\\\\n\\midrule\n1 & a \\\\\n\\bottomrule\n\\end{tabular}\n'

Issue Description

The resulting table includes the raw index name, instead of escaping properly.

Expected Behavior

import pandas as pd

pd.DataFrame({'_A': [1], '_B': ['a']}).set_index("_A").to_latex(escape=True)
>>'\\begin{tabular}{ll}\n\\toprule\n & \\_B \\\\\n\_A &  \\\\\n\\midrule\n1 & a \\\\\n\\bottomrule\n\\end{tabular}\n'

Installed Versions

INSTALLED VERSIONS

commit : f538741432edf55c6b9fb5d0d496d2dd1d7c2457 python : 3.10.8.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.22631 machine : AMD64 processor : Intel64 Family 6 Model 183 Stepping 1, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : de_DE.cp1252

pandas : 2.2.0 numpy : 1.26.2 pytz : 2023.3.post1 dateutil : 2.8.2 setuptools : 69.0.3 pip : 23.1.2 Cython : None pytest : 8.0.0 hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : None lxml.etree : 5.0.1 html5lib : None pymysql : None psycopg2 : None jinja2 : 3.1.2 IPython : 8.20.0 pandas_datareader : None adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : 4.12.2 bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : 2023.12.2 gcsfs : None matplotlib : None numba : None numexpr : None odfpy : None openpyxl : None pandas_gbq : None pyarrow : None pyreadstat : None python-calamine : None pyxlsb : None s3fs : None scipy : 1.11.4 sqlalchemy : None tables : None tabulate : None xarray : None xlrd : None zstandard : None tzdata : 2023.4 qtpy : None pyqt5 : None

attack68 commented 9 months ago

duplicated: #47489

attack68 commented 9 months ago

closest fix: #48936

quangngd commented 8 months ago

This can be fixed with

df1.to_latex(escape=True)

The document says

By default, the value will be read from the pandas config module and set to True if the option styler.format.escape is “latex”. When set to False prevents from escaping latex special characters in column names. Changed in version 2.0.0: The pandas option affecting this argument has changed, as has the default value to False.

Don't know why this relies on styler.format.escape. But default to True would be more sensible imo.

bileamScheuvens commented 8 months ago

This can be fixed with

df1.to_latex(escape=True)

Did you test this? The example includes

escape=True

47489 goes into more detail, this is definitely an issue.