Skip to content

Commit ab1a41a

Browse files
ri938TomAugspurger
authored andcommitted
BUG: render dataframe as html do not produce duplicate element id's (#16780) (#16801)
* BUG: when rendering dataframe as html do not produce duplicate element id's #16780 * CLN: removing spaces in code causes pylint check to fail * DOC: moved whatsnew comment to 0.20.3 release from 0.21.0 (cherry picked from commit 92e1cc8)
1 parent 7d2703d commit ab1a41a

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

doc/source/whatsnew/v0.20.3.txt

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Performance Improvements
3737
Bug Fixes
3838
~~~~~~~~~
3939
- Fixed issue with dataframe scatter plot for categorical data that reports incorrect column key not found when categorical data is used for plotting (:issue:`16199`)
40+
- Fixed issue with :meth:`DataFrame.style` where element id's were not unique (:issue:`16780`)
4041
- Fixed a pytest marker failing downstream packages' tests suites (:issue:`16680`)
4142
- Fixed compat with loading a ``DataFrame`` with a ``PeriodIndex``, from a ``format='fixed'`` HDFStore, in Python 3, that was written in Python 2 (:issue:`16781`)
4243
- Fixed a bug in failing to compute rolling computations of a column-MultiIndexed ``DataFrame`` (:issue:`16789`, :issue:`16825`)

pandas/io/formats/style.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,14 @@ def format_attr(pair):
281281
for r, idx in enumerate(self.data.index):
282282
row_es = []
283283
for c, value in enumerate(rlabels[r]):
284+
rid = [ROW_HEADING_CLASS, "level%s" % c, "row%s" % r]
284285
es = {
285286
"type": "th",
286287
"is_visible": _is_visible(r, c, idx_lengths),
287288
"value": value,
288289
"display_value": value,
289-
"class": " ".join([ROW_HEADING_CLASS, "level%s" % c,
290-
"row%s" % r]),
290+
"id": "_".join(rid[1:]),
291+
"class": " ".join(rid)
291292
}
292293
rowspan = idx_lengths.get((c, r), 0)
293294
if rowspan > 1:

pandas/tests/io/formats/test_style.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import textwrap
3+
import re
34

45
import pytest
56
import numpy as np
@@ -505,6 +506,14 @@ def test_uuid(self):
505506
assert result is styler
506507
assert result.uuid == 'aaa'
507508

509+
def test_unique_id(self):
510+
# See https://github.com/pandas-dev/pandas/issues/16780
511+
df = pd.DataFrame({'a': [1, 3, 5, 6], 'b': [2, 4, 12, 21]})
512+
result = df.style.render(uuid='test')
513+
assert 'test' in result
514+
ids = re.findall('id="(.*?)"', result)
515+
assert np.unique(ids).size == len(ids)
516+
508517
def test_table_styles(self):
509518
style = [{'selector': 'th', 'props': [('foo', 'bar')]}]
510519
styler = Styler(self.df, table_styles=style)
@@ -719,26 +728,29 @@ def test_mi_sparse(self):
719728
df = pd.DataFrame({'A': [1, 2]},
720729
index=pd.MultiIndex.from_arrays([['a', 'a'],
721730
[0, 1]]))
731+
722732
result = df.style._translate()
723733
body_0 = result['body'][0][0]
724734
expected_0 = {
725735
"value": "a", "display_value": "a", "is_visible": True,
726736
"type": "th", "attributes": ["rowspan=2"],
727-
"class": "row_heading level0 row0",
737+
"class": "row_heading level0 row0", "id": "level0_row0"
728738
}
729739
tm.assert_dict_equal(body_0, expected_0)
730740

731741
body_1 = result['body'][0][1]
732742
expected_1 = {
733743
"value": 0, "display_value": 0, "is_visible": True,
734744
"type": "th", "class": "row_heading level1 row0",
745+
"id": "level1_row0"
735746
}
736747
tm.assert_dict_equal(body_1, expected_1)
737748

738749
body_10 = result['body'][1][0]
739750
expected_10 = {
740751
"value": 'a', "display_value": 'a', "is_visible": False,
741752
"type": "th", "class": "row_heading level0 row1",
753+
"id": "level0_row1"
742754
}
743755
tm.assert_dict_equal(body_10, expected_10)
744756

0 commit comments

Comments
 (0)