Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] 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({"col": ["a", "b", "c"]})
df.style.hide([0,1]).format_index(escape="latex", axis=0).relabel_index(["$D$"]).to_latex()

df.style.hide([0,1]).relabel_index(["$D_{a}$"]).format_index(escape="latex", axis=0).to_latex()

# df.style.hide([0,1]).format_index(escape="latex", axis=0).relabel_index(["$D_{a}$"]).to_latex()

Issue Description

First version works flawlessly. Relabel index works and the output is

'\\begin{tabular}{ll}\n & col \\\\\n$D$ & c \\\\\n\\end{tabular}\n'

Second one is actually the desired one but does not work. I need to format the index in order to pass the correct escape parameter but the result is the old index:

'\\begin{tabular}{ll}\n & col \\\\\n2 & c \\\\\n\\end{tabular}\n'

Third version doesn't work at all since pandas tries to parse the a as a key.

Expected Behavior

I would expect the following string where the relabeled index is formatted with the correct escape sequence when using the second example:

'\\begin{tabular}{ll}\n & col \\\\\n$D_{a}$ & c \\\\\n\\end{tabular}\n'

Installed Versions

INSTALLED VERSIONS ------------------ commit : 478d340667831908b5b4bf09a2787a11a14560c9 python : 3.9.12.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.22621 machine : AMD64 processor : AMD64 Family 23 Model 104 Stepping 1, AuthenticAMD byteorder : little LC_ALL : None LANG : None LOCALE : de_DE.cp1252 pandas : 2.0.0 numpy : 1.22.4 pytz : 2022.1 dateutil : 2.8.2 setuptools : 65.6.3 pip : 22.2.2 Cython : None pytest : 7.2.0 hypothesis : None sphinx : 4.5.0 blosc : None feather : None xlsxwriter : 3.0.3 lxml.etree : 4.8.0 html5lib : None pymysql : None psycopg2 : None jinja2 : 3.1.2 IPython : 8.5.0 pandas_datareader: None bs4 : None bottleneck : None brotli : None fastparquet : None fsspec : None gcsfs : None matplotlib : 3.7.0 numba : None numexpr : 2.8.1 odfpy : None openpyxl : 3.0.9 pandas_gbq : None pyarrow : None pyreadstat : None pyxlsb : None s3fs : None scipy : 1.8.1 snappy : None sqlalchemy : None tables : None tabulate : None xarray : None xlrd : 1.2.0 zstandard : None tzdata : 2022.1 qtpy : None pyqt5 : None

Comment From: attack68

These methods are not designed to, and dont generally work together.

The documentation states the following and implies the two do not interoperate (although it could be clearer):

This method [relabel_index] should be used, rather than Styler.format_index(), in one of the following cases (see examples):

Comment From: attack68

In this case your would be better importing the escape_latex method and then creating a loop comprehension to label your index as you want.

new_labels = ["label1", "$&label2$"]
escaped_labels = [escape_latex(v) for v in new_labels]
styler.relabel_index(escaped_labels)

and not rely on format_index at all.

Comment From: ASchueddekopf

@attack68 ah okay. Thanks a lot. You are right, I read the docs but understood the line you cited not as that you can not combine them. I will look into the example you posted. Thanks!