Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add Series._repr_html_
Fixes #8829
  • Loading branch information
mrocklin committed Jul 4, 2019
commit a7596d69423a37aa3d773be1a5844417123b69d5
22 changes: 22 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,28 @@ def __repr__(self):

return result

def to_html(self):
text = self.to_frame().to_html()

lines = text.split('\n')
head_start = [i for i, line in enumerate(lines) if line.strip().startswith("<thead>")][0]
head_stop = [i for i, line in enumerate(lines) if line.strip().startswith("</thead>")][0]
del lines[head_start:head_stop]

tail = """
<tr>
<td> name: </td>
<th> %s </th>
</tr>
""" % (self.name or "--")
body_end = [i for i, line in enumerate(lines) if line.strip().startswith("</tbody>")][0]
lines[body_end:body_end] = tail.split('\n')

return '\n'.join(lines)

def _repr_html_(self):
return self.to_html()

def to_string(
self,
buf=None,
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,3 +713,14 @@ def test_to_html_with_col_space_units(unit):
for h in hdrs:
expected = '<th style="min-width: {unit};">'.format(unit=unit)
assert expected in h


def test_series():
df = DataFrame({'abc': range(1000)})

a = df.to_html()
b = df.abc.to_html()

assert a != b
assert 0.5 * len(b) < len(a) < 2 * len(b)
assert "abc" in b