Skip to content

BUG: index naming escaping not performed by Styler.format_index #47489

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
3 tasks done
attack68 opened this issue Jun 23, 2022 · 2 comments · Fixed by #57880
Closed
3 tasks done

BUG: index naming escaping not performed by Styler.format_index #47489

attack68 opened this issue Jun 23, 2022 · 2 comments · Fixed by #57880
Labels
Bug IO LaTeX to_latex Styler conditional formatting using DataFrame.style

Comments

@attack68
Copy link
Contributor

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

...

Issue Description

e.g. see https://stackoverflow.com/questions/72716879/is-there-a-function-to-format-the-index-name-in-a-pandas-styler-dataframe-style

Escaping is applied to formatted header cells but not to index name cells.

Expected Behavior

Unknown at this point, perhaps will work just to apply the same escape parameter to the index name but have to evaluate first.

Installed Versions

Replace this line with the output of pd.show_versions()

@attack68 attack68 added Bug Needs Triage Issue that has not been reviewed by a pandas team member Styler conditional formatting using DataFrame.style IO LaTeX to_latex and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 23, 2022
@teobucci
Copy link

Adding a few comments since I'm running in the same issue here (specifically I'm summarizing the results of some simulations and the names of the variables/scores I'm using contain many underscores and I'm exporting to LaTeX).

  • The correct import of the function is now from pandas.io.formats.style_render import _escape_latex.
  • I have a MultiIndex both in the index and in the columns, so the proposed fix df.index.name = _escape_latex(df.index.name) on Stack doesn't work for me since not all the names of the MultiIndex are different from None, therefore the .replace() function throws an error.

A working fix is the following:

from pandas.io.formats.style_render import _escape_latex

# assume your DataFrame is df
for level in range(df.index.nlevels):
    if df.index.get_level_values(level).name is not None:
        df.index.set_names(_escape_latex(df.index.get_level_values(level).name), level=level, inplace=True)
for level in range(df.columns.nlevels):
    if df.columns.get_level_values(level).name is not None:
        df.columns.set_names(_escape_latex(df.columns.get_level_values(level).name), level=level, inplace=True)
Installed versions

INSTALLED VERSIONS ------------------ commit : a671b5a python : 3.10.6.final.0 python-bits : 64 OS : Darwin OS-release : 21.6.0 Version : Darwin Kernel Version 21.6.0: Thu Nov 9 00:42:05 PST 2023; root:xnu-8020.240.18.705.10~1/RELEASE_ARM64_T6000 machine : arm64 processor : arm byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 2.1.4
numpy : 1.23.5
pytz : 2023.3
dateutil : 2.8.2
setuptools : 63.2.0
pip : 23.3.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.12.0
pandas_datareader : None
bs4 : 4.12.2
bottleneck : 1.3.7
dataframe-api-compat: None
fastparquet : None
fsspec : 2023.4.0
gcsfs : None
matplotlib : 3.7.1
numba : 0.56.4
numexpr : 2.9.0
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
sqlalchemy : None
tables : 3.9.2
tabulate : None
xarray : 2023.4.1
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@attack68
Copy link
Contributor Author

Thanks for the info.
This should be classed as a viable workaround in the meantime.

Note that this solution cannot be incorporated into Styler as a PR because it breaks the design objective of the styler as it modifies the DataFrame directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO LaTeX to_latex Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants