Skip to content

Commit 969fabe

Browse files
committed
ENH: pandas-dev#8750 add Series support for to_html and _repr_html_
1 parent 83795a3 commit 969fabe

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

doc/source/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ Serialization / IO / Conversion
729729
Series.to_json
730730
Series.to_sparse
731731
Series.to_dense
732+
Series.to_html
732733
Series.to_string
733734
Series.to_clipboard
734735

pandas/core/series.py

+57
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,63 @@ def __unicode__(self):
952952

953953
return result
954954

955+
def _repr_footer(self):
956+
957+
namestr = u("Name: %s, ") % com.pprint_thing(
958+
self.name) if self.name is not None else ""
959+
960+
# time series
961+
if self.is_time_series:
962+
if self.index.freq is not None:
963+
freqstr = u('Freq: %s, ') % self.index.freqstr
964+
else:
965+
freqstr = u('')
966+
967+
return u('%s%sLength: %d') % (freqstr, namestr, len(self))
968+
969+
# Categorical
970+
if com.is_categorical_dtype(self.dtype):
971+
level_info = self.values._repr_categories_info()
972+
return u('%sLength: %d, dtype: %s\n%s') % (namestr,
973+
len(self),
974+
str(self.dtype.name),
975+
level_info)
976+
977+
# reg series
978+
return u('%sLength: %d, dtype: %s') % (namestr,
979+
len(self),
980+
str(self.dtype.name))
981+
982+
def _repr_html_(self, *args, **kwargs):
983+
df = self.to_frame()
984+
if self.name is None:
985+
df.columns = ['']
986+
return df._repr_html_(*args, **kwargs)
987+
988+
def to_html(self, *args, **kwargs):
989+
"""
990+
Render a Series as an HTML table.
991+
992+
`to_html`-specific options:
993+
994+
bold_rows : boolean, default True
995+
Make the row labels bold in the output
996+
classes : str or list or tuple, default None
997+
CSS class(es) to apply to the resulting html table
998+
escape : boolean, default True
999+
Convert the characters <, >, and & to HTML-safe sequences.=
1000+
max_rows : int, optional
1001+
Maximum number of rows to show before truncating. If None, show
1002+
all.
1003+
max_cols : int, optional
1004+
Maximum number of columns to show before truncating. If None, show
1005+
all.
1006+
"""
1007+
df = self.to_frame()
1008+
if self.name is None:
1009+
df.columns = ['']
1010+
return df.to_html(*args, **kwargs)
1011+
9551012
def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True,
9561013
length=False, dtype=False, name=False, max_rows=None):
9571014
"""

pandas/tests/test_format.py

+63
Original file line numberDiff line numberDiff line change
@@ -3574,6 +3574,69 @@ def test_to_string_header(self):
35743574
exp = '0 0\n ..\n9 9'
35753575
self.assertEqual(res, exp)
35763576

3577+
def test_to_html(self):
3578+
expected_template = '''\
3579+
<table border="1" class="dataframe">
3580+
<thead>
3581+
<tr style="text-align: right;">
3582+
<th></th>
3583+
<th>{column_name}</th>
3584+
</tr>
3585+
</thead>
3586+
<tbody>
3587+
<tr>
3588+
<th>0</th>
3589+
<td>0</td>
3590+
</tr>
3591+
<tr>
3592+
<th>1</th>
3593+
<td>1</td>
3594+
</tr>
3595+
</tbody>
3596+
</table>'''
3597+
column_representations = {
3598+
'foo': 'foo',
3599+
None: '',
3600+
}
3601+
for series_name, column_name in column_representations.items():
3602+
s = pd.Series(range(2), dtype='int64', name=series_name)
3603+
result = s.to_html()
3604+
expected = expected_template.format(column_name=column_name)
3605+
self.assertEqual(result, expected)
3606+
3607+
def test_repr_html(self):
3608+
s = pd.Series(range(5), dtype='int64', name='foo')
3609+
self.assertTrue(hasattr(s, '_repr_html_'))
3610+
fmt.set_option('display.max_rows', 2)
3611+
result = s._repr_html_()
3612+
expected = u'''\
3613+
<div{div_style}>
3614+
<table border="1" class="dataframe">
3615+
<thead>
3616+
<tr style="text-align: right;">
3617+
<th></th>
3618+
<th>foo</th>
3619+
</tr>
3620+
</thead>
3621+
<tbody>
3622+
<tr>
3623+
<th>0</th>
3624+
<td>0</td>
3625+
</tr>
3626+
<tr>
3627+
<th>...</th>
3628+
<td>...</td>
3629+
</tr>
3630+
<tr>
3631+
<th>4</th>
3632+
<td>4</td>
3633+
</tr>
3634+
</tbody>
3635+
</table>
3636+
<p>5 rows \xd7 1 columns</p>
3637+
</div>'''.format(div_style=div_style)
3638+
self.assertEqual(result, expected)
3639+
35773640

35783641
class TestEngFormatter(tm.TestCase):
35793642
_multiprocess_can_split_ = True

0 commit comments

Comments
 (0)