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
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!