Skip to content

ENH: multicol naive implementation. part2 #43382

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 7 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ def register_converter_cb(key):
"latex.multicol_align",
"r",
styler_multicol_align,
validator=is_one_of_factory(["r", "c", "l"]),
validator=is_one_of_factory(["r", "c", "l", "naive-l", "naive-r"]),
)

cf.register_option(
Expand Down
7 changes: 5 additions & 2 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,13 @@ def to_latex(
without multirow.

.. versionchanged:: 1.4.0
multicol_align : {"r", "c", "l"}, optional
multicol_align : {"r", "c", "l", "naive-l", "naive-r"}, optional
If sparsifying hierarchical MultiIndex columns whether to align text at
the left, centrally, or at the right. If not given defaults to
``pandas.options.styler.latex.multicol_align``
``pandas.options.styler.latex.multicol_align``. If a naive option is
given renders without multicol.

.. versionchanged:: 1.4.0
siunitx : bool, default False
Set to ``True`` to structure LaTeX compatible with the {siunitx} package.
environment : str, optional
Expand Down
8 changes: 8 additions & 0 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,14 @@ def _parse_latex_header_span(
if 'colspan="' in attrs:
colspan = attrs[attrs.find('colspan="') + 9 :] # len('colspan="') = 9
colspan = int(colspan[: colspan.find('"')])
if "naive-l" == multicol_align:
out = f"{{{display_val}}}" if wrap else f"{display_val}"
Copy link
Member

Choose a reason for hiding this comment

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

Is this wrap related to siunitx?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes. the only time wrap is needed is when siunitx is True, but it only applies to columns headers, and not row headers, due to siunitx package establishing display properties for columns

blanks = " & {}" if wrap else " &"
return out + blanks * (colspan - 1)
elif "naive-r" == multicol_align:
out = f"{{{display_val}}}" if wrap else f"{display_val}"
blanks = "{} & " if wrap else "& "
return blanks * (colspan - 1) + out
return f"\\multicolumn{{{colspan}}}{{{multicol_align}}}{{{display_val}}}"
elif 'rowspan="' in attrs:
if multirow_align == "naive":
Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/io/formats/style/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,35 @@ def test_multiindex_row_and_col(df_ext):
assert result == expected


@pytest.mark.parametrize(
"multicol_align, siunitx, header",
[
("naive-l", False, " & A & &"),
("naive-r", False, " & & & A"),
("naive-l", True, "{} & {A} & {} & {}"),
("naive-r", True, "{} & {} & {} & {A}"),
],
)
def test_multicol_naive(df, multicol_align, siunitx, header):
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("A", "c")])
df.columns = ridx
level1 = " & a & b & c" if not siunitx else "{} & {a} & {b} & {c}"
Copy link
Member

Choose a reason for hiding this comment

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

I am not a fan of having logic inside tests.
I would suggest to move level1 to pytest.mark.parametrize alongside siunitx.
But it may not fit nicely in one line with the other parameters, so may be leave it as it is.

col_format = "lrrl" if not siunitx else "lSSl"
expected = dedent(
f"""\
\\begin{{tabular}}{{{col_format}}}
{header} \\\\
{level1} \\\\
0 & 0 & -0.61 & ab \\\\
1 & 1 & -1.22 & cd \\\\
\\end{{tabular}}
"""
)
styler = df.style.format(precision=2)
result = styler.to_latex(multicol_align=multicol_align, siunitx=siunitx)
assert expected == result


def test_multi_options(df_ext):
cidx = MultiIndex.from_tuples([("Z", "a"), ("Z", "b"), ("Y", "c")])
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("B", "c")])
Expand Down