Skip to content

Show Index Headers on DataFrames with Style #12090

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 1 commit into from
Jan 20, 2016
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
18 changes: 18 additions & 0 deletions pandas/core/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
34 changes: 34 additions & 0 deletions pandas/tests/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down