From cd4c180a1946f4a57f82d9bf7f1cce8818a58705 Mon Sep 17 00:00:00 2001 From: Allen Downey Date: Tue, 11 Jul 2017 19:29:15 -0400 Subject: [PATCH 1/2] Adding Series._repr_html_ --- pandas/core/series.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/core/series.py b/pandas/core/series.py index e1f668dd3afda..333c7e0d2587a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -6,6 +6,7 @@ # pylint: disable=E1101,E1103 # pylint: disable=W0703,W0622,W0613,W0201 +import re import types import warnings from textwrap import dedent @@ -1048,6 +1049,16 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True, with open(buf, 'w') as f: f.write(result) + def _repr_html_(self): + """ + Return a html representation for a particular Series. + Mainly for IPython notebook. + """ + df = self.to_frame() + s = df._repr_html_() + s2 = re.sub(r'(.*)', '', s, flags=re.DOTALL) + return s2 + def __iter__(self): """ provide iteration over the values of the Series box values if necessary """ From 4875a3dcc23fac851627c0c6b93ded9d6b1aca5a Mon Sep 17 00:00:00 2001 From: Allen Downey Date: Wed, 12 Jul 2017 09:45:40 -0400 Subject: [PATCH 2/2] Adding series_html_demo.ipynb --- series_html_demo.ipynb | 228 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 series_html_demo.ipynb diff --git a/series_html_demo.ipynb b/series_html_demo.ipynb new file mode 100644 index 0000000000000..9bc6fbe95ad10 --- /dev/null +++ b/series_html_demo.ipynb @@ -0,0 +1,228 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "series = pd.Series([])" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "series[0] = 'zero'\n", + "series[1] = 'one'" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
0zero
1one
\n", + "
" + ], + "text/plain": [ + "0 zero\n", + "1 one\n", + "dtype: object" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "series" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
0zero
1one
\n", + "
\n" + ] + } + ], + "source": [ + "print(series._repr_html_())" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "df = series.to_frame()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
0
0zero
1one
\n", + "
" + ], + "text/plain": [ + " 0\n", + "0 zero\n", + "1 one" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}