Skip to content

Backport PR #38533 on branch 1.2.x (BUG&TST: HTML formatting error in Styler.render() in rowspan attribute) #38640

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 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
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,10 @@ Plotting
- Bug in :meth:`DataFrame.plot` and :meth:`Series.plot` was overwriting matplotlib's shared y axes behaviour when no ``sharey`` parameter was passed (:issue:`37942`)
- Bug in :meth:`DataFrame.plot` was raising a ``TypeError`` with ``ExtensionDtype`` columns (:issue:`32073`)

Styler
^^^^^^

- Bug in :meth:`Styler.render` HTML was generated incorrectly beacause of formatting error in rowspan attribute, it now matches with w3 syntax. (:issue:`38234`)

Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def format_attr(pair):
rowspan = idx_lengths.get((c, r), 0)
if rowspan > 1:
es["attributes"] = [
format_attr({"key": "rowspan", "value": rowspan})
format_attr({"key": "rowspan", "value": f'"{rowspan}"'})
]
row_es.append(es)

Expand Down
11 changes: 10 additions & 1 deletion pandas/tests/io/formats/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,7 @@ def test_mi_sparse(self):
"display_value": "a",
"is_visible": True,
"type": "th",
"attributes": ["rowspan=2"],
"attributes": ['rowspan="2"'],
"class": "row_heading level0 row0",
"id": "level0_row0",
}
Expand Down Expand Up @@ -1740,6 +1740,15 @@ def test_colspan_w3(self):
s = Styler(df, uuid="_", cell_ids=False)
assert '<th class="col_heading level0 col0" colspan="2">l0</th>' in s.render()

def test_rowspan_w3(self):
# GH 38533
df = DataFrame(data=[[1, 2]], index=[["l0", "l0"], ["l1a", "l1b"]])
s = Styler(df, uuid="_", cell_ids=False)
assert (
'<th id="T___level0_row0" class="row_heading '
'level0 row0" rowspan="2">l0</th>' in s.render()
)

@pytest.mark.parametrize("len_", [1, 5, 32, 33, 100])
def test_uuid_len(self, len_):
# GH 36345
Expand Down