Skip to content

ENH: #8750 add Series support for to_html and _repr_html_ #10300

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

Closed
wants to merge 1 commit into from
Closed
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/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ Serialization / IO / Conversion
Series.to_json
Series.to_sparse
Series.to_dense
Series.to_html
Series.to_string
Series.to_clipboard

Expand Down
57 changes: 57 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,63 @@ def __unicode__(self):

return result

def _repr_footer(self):

namestr = u("Name: %s, ") % com.pprint_thing(
self.name) if self.name is not None else ""

# time series
if self.is_time_series:
if self.index.freq is not None:
freqstr = u('Freq: %s, ') % self.index.freqstr
else:
freqstr = u('')

return u('%s%sLength: %d') % (freqstr, namestr, len(self))

# Categorical
if com.is_categorical_dtype(self.dtype):
level_info = self.values._repr_categories_info()
return u('%sLength: %d, dtype: %s\n%s') % (namestr,
len(self),
str(self.dtype.name),
level_info)

# reg series
return u('%sLength: %d, dtype: %s') % (namestr,
len(self),
str(self.dtype.name))

def _repr_html_(self, *args, **kwargs):
df = self.to_frame()
if self.name is None:
df.columns = ['']
return df._repr_html_(*args, **kwargs)

def to_html(self, *args, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add docstring, then add it to api.rst. Better to use explicit keywords to render API doc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

"""
Render a Series as an HTML table.

`to_html`-specific options:

bold_rows : boolean, default True
Make the row labels bold in the output
classes : str or list or tuple, default None
CSS class(es) to apply to the resulting html table
escape : boolean, default True
Convert the characters <, >, and & to HTML-safe sequences.=
max_rows : int, optional
Maximum number of rows to show before truncating. If None, show
all.
max_cols : int, optional
Maximum number of columns to show before truncating. If None, show
all.
"""
df = self.to_frame()
if self.name is None:
df.columns = ['']
return df.to_html(*args, **kwargs)

def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True,
length=False, dtype=False, name=False, max_rows=None):
"""
Expand Down
63 changes: 63 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3574,6 +3574,69 @@ def test_to_string_header(self):
exp = '0 0\n ..\n9 9'
self.assertEqual(res, exp)

def test_to_html(self):
expected_template = '''\
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>{column_name}</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
</tr>
</tbody>
</table>'''
column_representations = {
'foo': 'foo',
None: '',
}
for series_name, column_name in column_representations.items():
s = pd.Series(range(2), dtype='int64', name=series_name)
result = s.to_html()
expected = expected_template.format(column_name=column_name)
self.assertEqual(result, expected)

def test_repr_html(self):
s = pd.Series(range(5), dtype='int64', name='foo')
self.assertTrue(hasattr(s, '_repr_html_'))
fmt.set_option('display.max_rows', 2)
result = s._repr_html_()
expected = u'''\
<div{div_style}>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>foo</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
</tr>
<tr>
<th>...</th>
<td>...</td>
</tr>
<tr>
<th>4</th>
<td>4</td>
</tr>
</tbody>
</table>
<p>5 rows \xd7 1 columns</p>
</div>'''.format(div_style=div_style)
self.assertEqual(result, expected)


class TestEngFormatter(tm.TestCase):
_multiprocess_can_split_ = True
Expand Down