diff --git a/pandas/core/series.py b/pandas/core/series.py index b3a7f38aef8ef..eb78c5f6d2dcb 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1611,6 +1611,32 @@ def __repr__(self): return result + def _repr_html_(self): + text = self.to_frame()._repr_html_() + + lines = text.split("\n") + head_start = [ + i for i, line in enumerate(lines) if line.strip().startswith("") + ][0] + head_stop = [ + i for i, line in enumerate(lines) if line.strip().startswith("") + ][0] + del lines[head_start:head_stop] + + tail = """ + + name: + %s + + """ % str(self.name) or "--" + + body_end = [ + i for i, line in enumerate(lines) if line.strip().startswith("") + ][0] + lines[body_end:body_end] = tail.split("\n") + + return "\n".join(lines) + def to_string( self, buf=None, diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index af862b11c756c..c3bd103120ad2 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -1769,6 +1769,24 @@ def test_repr_html(self, float_frame): tm.reset_display_options() + def test_series(series): + df = DataFrame({"abc": range(1000)}) + + a = df._repr_html_() + b = df.abc._repr_html_() + + assert a != b + assert 0.5 * len(b) < len(a) < 2 * len(b) + assert "abc" in b + + s = Series([123, 456], name=(1, 2)) + assert "123" in s._repr_html_() + assert "(1, 2)" in s._repr_html_() + + lines = s._repr_html_().split('\n') + indents = [line.index("") for line in lines if "" in line] + assert len(set(indents)) == 1 # cleanly indented HTML + def test_repr_html_mathjax(self): df = DataFrame([[1, 2], [3, 4]]) assert "tex2jax_ignore" not in df._repr_html_()