Skip to content

Commit b1d8a27

Browse files
committed
amend tests for siunitx
1 parent 8ea7b0c commit b1d8a27

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

pandas/io/formats/style_render.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,9 +1412,13 @@ def _parse_latex_header_span(
14121412
colspan = attrs[attrs.find('colspan="') + 9 :] # len('colspan="') = 9
14131413
colspan = int(colspan[: colspan.find('"')])
14141414
if "naive-l" == multicol_align:
1415-
return f"{{{display_val}}}" + " &" * (colspan - 1)
1415+
out = f"{{{display_val}}}" if wrap else f"{display_val}"
1416+
blanks = " & {}" if wrap else " &"
1417+
return out + blanks * (colspan - 1)
14161418
elif "naive-r" == multicol_align:
1417-
return "& " * (colspan - 1) + f"{{{display_val}}}"
1419+
out = f"{{{display_val}}}" if wrap else f"{display_val}"
1420+
blanks = "{} & " if wrap else "& "
1421+
return blanks * (colspan - 1) + out
14181422
return f"\\multicolumn{{{colspan}}}{{{multicol_align}}}{{{display_val}}}"
14191423
elif 'rowspan="' in attrs:
14201424
rowspan = attrs[attrs.find('rowspan="') + 9 :]

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,24 +278,31 @@ def test_multiindex_row_and_col(df):
278278

279279

280280
@pytest.mark.parametrize(
281-
"multicol_align, exp", [("naive-l", "{A} & &"), ("naive-r", "& & {A}")]
281+
"multicol_align, siunitx, exp",
282+
[
283+
("naive-l", False, " & A & &"),
284+
("naive-r", False, " & & & A"),
285+
("naive-l", True, "{} & {A} & {} & {}"),
286+
("naive-r", True, "{} & {} & {} & {A}"),
287+
],
282288
)
283-
def test_multicol_naive(df, multicol_align, exp):
289+
def test_multicol_naive(df, multicol_align, siunitx, exp):
284290
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("A", "c")])
285291
df = df.astype({"A": int})
286292
df.columns = ridx
293+
level1 = " & a & b & c" if not siunitx else "{} & {a} & {b} & {c}"
287294
expected = dedent(
288295
f"""\
289-
\\begin{{tabular}}{{lrrl}}
290-
{{}} & {exp} \\\\
291-
{{}} & {{a}} & {{b}} & {{c}} \\\\
296+
\\begin{{tabular}}{{l{"SS" if siunitx else "rr"}l}}
297+
{exp} \\\\
298+
{level1} \\\\
292299
0 & 0 & -0.61 & ab \\\\
293300
1 & 1 & -1.22 & cd \\\\
294301
\\end{{tabular}}
295302
"""
296303
)
297304
s = df.style.format(precision=2)
298-
assert expected == s.to_latex(multicol_align=multicol_align)
305+
assert expected == s.to_latex(multicol_align=multicol_align, siunitx=siunitx)
299306

300307

301308
def test_multi_options(df):

0 commit comments

Comments
 (0)