Skip to content

Commit 4e3bcb6

Browse files
authored
add css conversion for new apply_index (#43657)
Co-authored-by: JHM Darbyshire (iMac) <[email protected]>
1 parent 23f5ce1 commit 4e3bcb6

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

pandas/io/formats/style_render.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,11 @@ def _parse_latex_cell_styles(
16361636

16371637

16381638
def _parse_latex_header_span(
1639-
cell: dict[str, Any], multirow_align: str, multicol_align: str, wrap: bool = False
1639+
cell: dict[str, Any],
1640+
multirow_align: str,
1641+
multicol_align: str,
1642+
wrap: bool = False,
1643+
convert_css: bool = False,
16401644
) -> str:
16411645
r"""
16421646
Refactor the cell `display_value` if a 'colspan' or 'rowspan' attribute is present.
@@ -1657,7 +1661,9 @@ def _parse_latex_header_span(
16571661
>>> _parse_latex_header_span(cell, 't', 'c')
16581662
'\\multicolumn{3}{c}{text}'
16591663
"""
1660-
display_val = _parse_latex_cell_styles(cell["cellstyle"], cell["display_value"])
1664+
display_val = _parse_latex_cell_styles(
1665+
cell["cellstyle"], cell["display_value"], convert_css
1666+
)
16611667
if "attributes" in cell:
16621668
attrs = cell["attributes"]
16631669
if 'colspan="' in attrs:

pandas/io/formats/templates/latex_table.tpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
\{{toprule}}
3232
{% endif %}
3333
{% for row in head %}
34-
{% for c in row %}{%- if not loop.first %} & {% endif %}{{parse_header(c, multirow_align, multicol_align, siunitx)}}{% endfor %} \\
34+
{% for c in row %}{%- if not loop.first %} & {% endif %}{{parse_header(c, multirow_align, multicol_align, siunitx, convert_css)}}{% endfor %} \\
3535
{% endfor %}
3636
{% set midrule = parse_table(table_styles, 'midrule') %}
3737
{% if midrule is not none %}
3838
\{{midrule}}
3939
{% endif %}
4040
{% for row in body %}
4141
{% for c in row %}{% if not loop.first %} & {% endif %}
42-
{%- if c.type == 'th' %}{{parse_header(c, multirow_align, multicol_align)}}{% else %}{{parse_cell(c.cellstyle, c.display_value, convert_css)}}{% endif %}
42+
{%- if c.type == 'th' %}{{parse_header(c, multirow_align, multicol_align, False, convert_css)}}{% else %}{{parse_cell(c.cellstyle, c.display_value, convert_css)}}{% endif %}
4343
{%- endfor %} \\
4444
{% endfor %}
4545
{% set bottomrule = parse_table(table_styles, 'bottomrule') %}

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

+7
Original file line numberDiff line numberDiff line change
@@ -782,3 +782,10 @@ def test_repr_option(styler):
782782
def test_siunitx_basic_headers(styler):
783783
assert "{} & {A} & {B} & {C} \\\\" in styler.to_latex(siunitx=True)
784784
assert " & A & B & C \\\\" in styler.to_latex() # default siunitx=False
785+
786+
787+
@pytest.mark.parametrize("axis", ["index", "columns"])
788+
def test_css_convert_apply_index(styler, axis):
789+
styler.applymap_index(lambda x: "font-weight: bold;", axis=axis)
790+
for label in getattr(styler, axis):
791+
assert f"\\bfseries {label}" in styler.to_latex(convert_css=True)

0 commit comments

Comments
 (0)