From 9c24aa68d8ea6390ef2b819f2a2d524e4761cc9c Mon Sep 17 00:00:00 2001 From: Henry Hammond Date: Tue, 19 Jan 2016 10:50:44 -0500 Subject: [PATCH] Added index column header for styles --- doc/source/whatsnew/v0.18.0.txt | 1 + pandas/core/style.py | 18 +++++++++++++++++ pandas/tests/test_style.py | 34 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index db9cf5ae86d39..3fce0939445d7 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -538,3 +538,4 @@ of columns didn't match the number of series provided (:issue:`12039`). - Bug in ``Series`` constructor with read-only data (:issue:`11502`) - Bug in ``.loc`` setitem indexer preventing the use of a TZ-aware DatetimeIndex (:issue:`12050`) +- Big in ``.style`` indexes and multi-indexes not appearing (:issue:`11655`) diff --git a/pandas/core/style.py b/pandas/core/style.py index d8cb53e04ea03..203eda4fdf338 100644 --- a/pandas/core/style.py +++ b/pandas/core/style.py @@ -206,6 +206,24 @@ def _translate(self): "class": " ".join(cs)}) head.append(row_es) + if self.data.index.names: + index_header_row = [] + + for c, name in enumerate(self.data.index.names): + cs = [COL_HEADING_CLASS, + "level%s" % (n_clvls + 1), + "col%s" % c] + index_header_row.append({"type": "th", "value": name, + "class": " ".join(cs)}) + + index_header_row.extend( + [{"type": "th", + "value": BLANK_VALUE, + "class": " ".join([BLANK_CLASS]) + }] * len(clabels[0])) + + head.append(index_header_row) + body = [] for r, idx in enumerate(self.data.index): cs = [ROW_HEADING_CLASS, "level%s" % c, "row%s" % r] diff --git a/pandas/tests/test_style.py b/pandas/tests/test_style.py index fd8540fdf9c0a..b9ca3f331711d 100644 --- a/pandas/tests/test_style.py +++ b/pandas/tests/test_style.py @@ -130,6 +130,40 @@ def test_set_properties_subset(self): expected = {(0, 0): ['color: white']} self.assertEqual(result, expected) + def test_index_name(self): + # https://github.com/pydata/pandas/issues/11655 + df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]}) + result = df.set_index('A').style._translate() + + expected = [[{'class': 'blank', 'type': 'th', 'value': ''}, + {'class': 'col_heading level0 col0', 'type': 'th', + 'value': 'B'}, + {'class': 'col_heading level0 col1', 'type': 'th', + 'value': 'C'}], + [{'class': 'col_heading level2 col0', 'type': 'th', + 'value': 'A'}, + {'class': 'blank', 'type': 'th', 'value': ''}, + {'class': 'blank', 'type': 'th', 'value': ''}]] + + self.assertEqual(result['head'], expected) + + def test_multiindex_name(self): + # https://github.com/pydata/pandas/issues/11655 + df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]}) + result = df.set_index(['A', 'B']).style._translate() + + expected = [[{'class': 'blank', 'type': 'th', 'value': ''}, + {'class': 'blank', 'type': 'th', 'value': ''}, + {'class': 'col_heading level0 col0', 'type': 'th', + 'value': 'C'}], + [{'class': 'col_heading level2 col0', 'type': 'th', + 'value': 'A'}, + {'class': 'col_heading level2 col1', 'type': 'th', + 'value': 'B'}, + {'class': 'blank', 'type': 'th', 'value': ''}]] + + self.assertEqual(result['head'], expected) + def test_apply_axis(self): df = pd.DataFrame({'A': [0, 0], 'B': [1, 1]}) f = lambda x: ['val: %s' % x.max() for v in x]