Skip to content

Commit 45982d8

Browse files
authored
ENH: multicol naive implementation. part2 (#43382)
* multirow naive implementation * amend tests for siunitx * ivanovmg req Co-authored-by: JHM Darbyshire (iMac) <[email protected]>
1 parent 6e06f89 commit 45982d8

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

pandas/core/config_init.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ def register_converter_cb(key):
896896
"latex.multicol_align",
897897
"r",
898898
styler_multicol_align,
899-
validator=is_one_of_factory(["r", "c", "l"]),
899+
validator=is_one_of_factory(["r", "c", "l", "naive-l", "naive-r"]),
900900
)
901901

902902
cf.register_option(

pandas/io/formats/style.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,13 @@ def to_latex(
527527
without multirow.
528528
529529
.. versionchanged:: 1.4.0
530-
multicol_align : {"r", "c", "l"}, optional
530+
multicol_align : {"r", "c", "l", "naive-l", "naive-r"}, optional
531531
If sparsifying hierarchical MultiIndex columns whether to align text at
532532
the left, centrally, or at the right. If not given defaults to
533-
``pandas.options.styler.latex.multicol_align``
533+
``pandas.options.styler.latex.multicol_align``. If a naive option is
534+
given renders without multicol.
535+
536+
.. versionchanged:: 1.4.0
534537
siunitx : bool, default False
535538
Set to ``True`` to structure LaTeX compatible with the {siunitx} package.
536539
environment : str, optional

pandas/io/formats/style_render.py

+8
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,14 @@ def _parse_latex_header_span(
14101410
if 'colspan="' in attrs:
14111411
colspan = attrs[attrs.find('colspan="') + 9 :] # len('colspan="') = 9
14121412
colspan = int(colspan[: colspan.find('"')])
1413+
if "naive-l" == multicol_align:
1414+
out = f"{{{display_val}}}" if wrap else f"{display_val}"
1415+
blanks = " & {}" if wrap else " &"
1416+
return out + blanks * (colspan - 1)
1417+
elif "naive-r" == multicol_align:
1418+
out = f"{{{display_val}}}" if wrap else f"{display_val}"
1419+
blanks = "{} & " if wrap else "& "
1420+
return blanks * (colspan - 1) + out
14131421
return f"\\multicolumn{{{colspan}}}{{{multicol_align}}}{{{display_val}}}"
14141422
elif 'rowspan="' in attrs:
14151423
if multirow_align == "naive":

pandas/tests/io/formats/style/test_to_latex.py

+29
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,35 @@ def test_multiindex_row_and_col(df_ext):
302302
assert result == expected
303303

304304

305+
@pytest.mark.parametrize(
306+
"multicol_align, siunitx, header",
307+
[
308+
("naive-l", False, " & A & &"),
309+
("naive-r", False, " & & & A"),
310+
("naive-l", True, "{} & {A} & {} & {}"),
311+
("naive-r", True, "{} & {} & {} & {A}"),
312+
],
313+
)
314+
def test_multicol_naive(df, multicol_align, siunitx, header):
315+
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("A", "c")])
316+
df.columns = ridx
317+
level1 = " & a & b & c" if not siunitx else "{} & {a} & {b} & {c}"
318+
col_format = "lrrl" if not siunitx else "lSSl"
319+
expected = dedent(
320+
f"""\
321+
\\begin{{tabular}}{{{col_format}}}
322+
{header} \\\\
323+
{level1} \\\\
324+
0 & 0 & -0.61 & ab \\\\
325+
1 & 1 & -1.22 & cd \\\\
326+
\\end{{tabular}}
327+
"""
328+
)
329+
styler = df.style.format(precision=2)
330+
result = styler.to_latex(multicol_align=multicol_align, siunitx=siunitx)
331+
assert expected == result
332+
333+
305334
def test_multi_options(df_ext):
306335
cidx = MultiIndex.from_tuples([("Z", "a"), ("Z", "b"), ("Y", "c")])
307336
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("B", "c")])

0 commit comments

Comments
 (0)