Skip to content

EHN: df.to_latex(escape=True) also escape index names #61307

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

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3568,6 +3568,7 @@ def _wrap(x, alt_format_):
elif formatters is None and float_format is not None:
formatters_ = partial(_wrap, alt_format_=lambda v: v)
format_index_ = [index_format_, column_format_]
format_index_names_ = [index_format_, column_format_]

# Deal with hiding indexes and relabelling column names
hide_: list[dict] = []
Expand All @@ -3584,6 +3585,7 @@ def _wrap(x, alt_format_):
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
Copy link
Member

@rhshadrach rhshadrach Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense. I updated the code

Copy link
Contributor

@attack68 attack68 Apr 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test added


if index is False:
hide_.append({"axis": "index"})
Expand Down Expand Up @@ -3616,6 +3618,7 @@ def _wrap(x, alt_format_):
relabel_index=relabel_index_,
format={"formatter": formatters_, **base_format_},
format_index=format_index_,
format_index_names=format_index_names_,
render_kwargs=render_kwargs_,
)

Expand All @@ -3628,6 +3631,7 @@ def _to_latex_via_styler(
relabel_index: dict | list[dict] | None = None,
format: dict | list[dict] | None = None,
format_index: dict | list[dict] | None = None,
format_index_names: dict | list[dict] | None = None,
render_kwargs: dict | None = None,
):
"""
Expand Down Expand Up @@ -3672,7 +3676,13 @@ def _to_latex_via_styler(
self = cast("DataFrame", self)
styler = Styler(self, uuid="")

for kw_name in ["hide", "relabel_index", "format", "format_index"]:
for kw_name in [
"hide",
"relabel_index",
"format",
"format_index",
"format_index_names",
]:
kw = vars()[kw_name]
if isinstance(kw, dict):
getattr(styler, kw_name)(**kw)
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/io/formats/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,27 @@ def test_to_latex_escape_special_chars(self):
)
assert result == expected

def test_to_latex_escape_special_chars_in_index_names(self):
# https://github.com/pandas-dev/pandas/issues/61309
# https://github.com/pandas-dev/pandas/issues/57362
index = "&%$#_{}}~^\\"
df = DataFrame({index: [1, 2, 3]}).set_index(index)
result = df.to_latex(escape=True)
expected = _dedent(
r"""
\begin{tabular}{l}
\toprule
\&\%\$\#\_\{\}\}\textasciitilde \textasciicircum \textbackslash \\
\midrule
1 \\
2 \\
3 \\
\bottomrule
\end{tabular}
"""
)
assert result == expected

def test_to_latex_specified_header_special_chars_without_escape(self):
# GH 7124
df = DataFrame({"a": [1, 2], "b": ["b1", "b2"]})
Expand Down
Loading