diff --git a/doc/source/api.rst b/doc/source/api.rst index 6bee0a1ceafb8..60e0e34c11db8 100644 --- a/doc/source/api.rst +++ b/doc/source/api.rst @@ -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 diff --git a/pandas/core/series.py b/pandas/core/series.py index 5106225cdd3c9..da9292a8f4758 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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): + """ + 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): """ diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py index 22555a84c55de..5886d6452f9e7 100644 --- a/pandas/tests/test_format.py +++ b/pandas/tests/test_format.py @@ -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 = '''\ +
+ | {column_name} | +
---|---|
0 | +0 | +
1 | +1 | +
+ | foo | +
---|---|
0 | +0 | +
... | +... | +
4 | +4 | +
5 rows \xd7 1 columns
+