EHN: df.to_latex(escape=True) also escape index names#61307
EHN: df.to_latex(escape=True) also escape index names#61307mroeschke merged 7 commits intopandas-dev:mainfrom
df.to_latex(escape=True) also escape index names#61307Conversation
| assert result == expected | ||
|
|
||
| def test_to_latex_escape_special_chars_in_index_names(self): | ||
| special_characters = ["&", "%", "$", "#", "_", "{", "}", "~", "^", "\\"] |
There was a problem hiding this comment.
Can you start the first line of the test with a reference to the issue:
# https://github.com/pandas-dev/pandas/issues/61309
|
cc @attack68 as the codeowner of styler |
pandas/core/generic.py
Outdated
| elif isinstance(header, (list, tuple)): | ||
| relabel_index_.append({"labels": header, "axis": "columns"}) | ||
| format_index_ = [index_format_] # column_format is overwritten | ||
| format_index_names_ = [index_format_] # column_format is overwritten |
There was a problem hiding this comment.
I don't think we want to do this here. Currently:
df = pd.DataFrame({'_A': [1], '_B': ['a']}).set_index("_A")
df.columns.name = "_C"
print(df.to_latex(escape=True, header=["_B"]))
# \begin{tabular}{ll}
# \toprule
# _C & _B \\
# \_A & \\
# \midrule
# 1 & a \\
# \bottomrule
# \end{tabular}whereas I think one would expect \_C.
There was a problem hiding this comment.
Make sense. I updated the code
There was a problem hiding this comment.
And also "_B", since it wont render in latex otherwise?
On second thought maybe this is ambiguous, and the code written already handles the alternative and expects a user to input their header override directly.
There was a problem hiding this comment.
the code written already handles the alternative and expects a user to input their header override directly
Agreed, if we were to pursue a change here, it should be a separate issue.
Can you add a test for columns.name as well.
|
Thanks @quangngd |
doc/source/whatsnew/vX.X.X.rstfile if fixing a bug or adding a new feature.Stage 3. Part of an multi-stages effort: #57880 (comment)
Part 2 has a issue at #59324
But in the process of implementing, I realized the current implementation of
df.to_latex(escape)does not go throughstyler.to_latexbut to callstyler.format_index. This implementation follows the same flow. So maybe part 2 is not really relevant.