Skip to content

Commit 9c24aa6

Browse files
committed
Added index column header for styles
1 parent 4182105 commit 9c24aa6

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,4 @@ of columns didn't match the number of series provided (:issue:`12039`).
538538
- Bug in ``Series`` constructor with read-only data (:issue:`11502`)
539539

540540
- Bug in ``.loc`` setitem indexer preventing the use of a TZ-aware DatetimeIndex (:issue:`12050`)
541+
- Big in ``.style`` indexes and multi-indexes not appearing (:issue:`11655`)

pandas/core/style.py

+18
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,24 @@ def _translate(self):
206206
"class": " ".join(cs)})
207207
head.append(row_es)
208208

209+
if self.data.index.names:
210+
index_header_row = []
211+
212+
for c, name in enumerate(self.data.index.names):
213+
cs = [COL_HEADING_CLASS,
214+
"level%s" % (n_clvls + 1),
215+
"col%s" % c]
216+
index_header_row.append({"type": "th", "value": name,
217+
"class": " ".join(cs)})
218+
219+
index_header_row.extend(
220+
[{"type": "th",
221+
"value": BLANK_VALUE,
222+
"class": " ".join([BLANK_CLASS])
223+
}] * len(clabels[0]))
224+
225+
head.append(index_header_row)
226+
209227
body = []
210228
for r, idx in enumerate(self.data.index):
211229
cs = [ROW_HEADING_CLASS, "level%s" % c, "row%s" % r]

pandas/tests/test_style.py

+34
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,40 @@ def test_set_properties_subset(self):
130130
expected = {(0, 0): ['color: white']}
131131
self.assertEqual(result, expected)
132132

133+
def test_index_name(self):
134+
# https://github.com/pydata/pandas/issues/11655
135+
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]})
136+
result = df.set_index('A').style._translate()
137+
138+
expected = [[{'class': 'blank', 'type': 'th', 'value': ''},
139+
{'class': 'col_heading level0 col0', 'type': 'th',
140+
'value': 'B'},
141+
{'class': 'col_heading level0 col1', 'type': 'th',
142+
'value': 'C'}],
143+
[{'class': 'col_heading level2 col0', 'type': 'th',
144+
'value': 'A'},
145+
{'class': 'blank', 'type': 'th', 'value': ''},
146+
{'class': 'blank', 'type': 'th', 'value': ''}]]
147+
148+
self.assertEqual(result['head'], expected)
149+
150+
def test_multiindex_name(self):
151+
# https://github.com/pydata/pandas/issues/11655
152+
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]})
153+
result = df.set_index(['A', 'B']).style._translate()
154+
155+
expected = [[{'class': 'blank', 'type': 'th', 'value': ''},
156+
{'class': 'blank', 'type': 'th', 'value': ''},
157+
{'class': 'col_heading level0 col0', 'type': 'th',
158+
'value': 'C'}],
159+
[{'class': 'col_heading level2 col0', 'type': 'th',
160+
'value': 'A'},
161+
{'class': 'col_heading level2 col1', 'type': 'th',
162+
'value': 'B'},
163+
{'class': 'blank', 'type': 'th', 'value': ''}]]
164+
165+
self.assertEqual(result['head'], expected)
166+
133167
def test_apply_axis(self):
134168
df = pd.DataFrame({'A': [0, 0], 'B': [1, 1]})
135169
f = lambda x: ['val: %s' % x.max() for v in x]

0 commit comments

Comments
 (0)