Skip to content

Commit

Permalink
BUG: render dataframe as html do not produce duplicate element id's (p…
Browse files Browse the repository at this point in the history
…andas-dev#16780) (pandas-dev#16801)

* BUG: when rendering dataframe as html do not produce duplicate element id's pandas-dev#16780

* CLN: removing spaces in code causes pylint check to fail

* DOC: moved whatsnew comment to 0.20.3 release from 0.21.0
  • Loading branch information
ri938 authored and TomAugspurger committed Jul 3, 2017
1 parent 329fdaa commit 92e1cc8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Performance Improvements
Bug Fixes
~~~~~~~~~
- 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`)
- Fixed issue with :meth:`DataFrame.style` where element id's were not unique (:issue:`16780`)
- Fixed a pytest marker failing downstream packages' tests suites (:issue:`16680`)
- 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`)

Expand Down
5 changes: 3 additions & 2 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,14 @@ def format_attr(pair):
for r, idx in enumerate(self.data.index):
row_es = []
for c, value in enumerate(rlabels[r]):
rid = [ROW_HEADING_CLASS, "level%s" % c, "row%s" % r]
es = {
"type": "th",
"is_visible": _is_visible(r, c, idx_lengths),
"value": value,
"display_value": value,
"class": " ".join([ROW_HEADING_CLASS, "level%s" % c,
"row%s" % r]),
"id": "_".join(rid[1:]),
"class": " ".join(rid)
}
rowspan = idx_lengths.get((c, r), 0)
if rowspan > 1:
Expand Down
14 changes: 13 additions & 1 deletion pandas/tests/io/formats/test_style.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import textwrap
import re

import pytest
import numpy as np
Expand Down Expand Up @@ -505,6 +506,14 @@ def test_uuid(self):
assert result is styler
assert result.uuid == 'aaa'

def test_unique_id(self):
# See https://github.com/pandas-dev/pandas/issues/16780
df = pd.DataFrame({'a': [1, 3, 5, 6], 'b': [2, 4, 12, 21]})
result = df.style.render(uuid='test')
assert 'test' in result
ids = re.findall('id="(.*?)"', result)
assert np.unique(ids).size == len(ids)

def test_table_styles(self):
style = [{'selector': 'th', 'props': [('foo', 'bar')]}]
styler = Styler(self.df, table_styles=style)
Expand Down Expand Up @@ -719,26 +728,29 @@ def test_mi_sparse(self):
df = pd.DataFrame({'A': [1, 2]},
index=pd.MultiIndex.from_arrays([['a', 'a'],
[0, 1]]))

result = df.style._translate()
body_0 = result['body'][0][0]
expected_0 = {
"value": "a", "display_value": "a", "is_visible": True,
"type": "th", "attributes": ["rowspan=2"],
"class": "row_heading level0 row0",
"class": "row_heading level0 row0", "id": "level0_row0"
}
tm.assert_dict_equal(body_0, expected_0)

body_1 = result['body'][0][1]
expected_1 = {
"value": 0, "display_value": 0, "is_visible": True,
"type": "th", "class": "row_heading level1 row0",
"id": "level1_row0"
}
tm.assert_dict_equal(body_1, expected_1)

body_10 = result['body'][1][0]
expected_10 = {
"value": 'a', "display_value": 'a', "is_visible": False,
"type": "th", "class": "row_heading level0 row1",
"id": "level0_row1"
}
tm.assert_dict_equal(body_10, expected_10)

Expand Down

0 comments on commit 92e1cc8

Please sign in to comment.