Skip to content

Commit 1bab75c

Browse files
committed
DOC: Show how Pandas dataframes can be displayed
1 parent 3e2fd54 commit 1bab75c

File tree

5 files changed

+117
-3
lines changed

5 files changed

+117
-3
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ install:
2727
- pip install Sphinx$SPHINX
2828
- pip install .
2929
- pip install ipykernel
30+
- pip install numpy
3031
- pip install matplotlib
32+
- pip install pandas
3133
- pip install sphinxcontrib-bibtex
3234
script:
3335
- python -m sphinx doc/ doc/_build/ -b html

doc/code-cells.ipynb

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@
160160
"source": [
161161
"## Special Display Formats\n",
162162
"\n",
163-
"See [IPython example notebook](https://nbviewer.jupyter.org/github/ipython/ipython/blob/master/examples/IPython Kernel/Rich Output.ipynb).\n",
164-
"\n",
165-
"TODO: tables? e.g. Pandas DataFrame?"
163+
"See [IPython example notebook](https://nbviewer.jupyter.org/github/ipython/ipython/blob/master/examples/IPython Kernel/Rich Output.ipynb)."
166164
]
167165
},
168166
{
@@ -402,6 +400,115 @@
402400
"fig"
403401
]
404402
},
403+
{
404+
"cell_type": "markdown",
405+
"metadata": {},
406+
"source": [
407+
"### Pandas Dataframes\n",
408+
"\n",
409+
"[Pandas dataframes](http://pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe)\n",
410+
"should be displayed as nicely formatted HTML tables (if you are using HTML output)."
411+
]
412+
},
413+
{
414+
"cell_type": "code",
415+
"execution_count": null,
416+
"metadata": {},
417+
"outputs": [],
418+
"source": [
419+
"import numpy as np\n",
420+
"import pandas as pd"
421+
]
422+
},
423+
{
424+
"cell_type": "code",
425+
"execution_count": null,
426+
"metadata": {},
427+
"outputs": [],
428+
"source": [
429+
"df = pd.DataFrame(np.random.randint(0, 100, size=[5, 4]),\n",
430+
" columns=['a', 'b', 'c', 'd'])\n",
431+
"df"
432+
]
433+
},
434+
{
435+
"cell_type": "markdown",
436+
"metadata": {},
437+
"source": [
438+
"For LaTeX output, however, the plain text output is used by default.\n",
439+
"\n",
440+
"To get nice LaTeX tables, a few settings have to be changed:"
441+
]
442+
},
443+
{
444+
"cell_type": "code",
445+
"execution_count": null,
446+
"metadata": {},
447+
"outputs": [],
448+
"source": [
449+
"pd.set_option('display.latex.repr', True)"
450+
]
451+
},
452+
{
453+
"cell_type": "markdown",
454+
"metadata": {},
455+
"source": [
456+
"This is not enabled by default because of\n",
457+
"[Pandas issue #12182](https://github.com/pandas-dev/pandas/issues/12182).\n",
458+
"\n",
459+
"The generated LaTeX tables utilize the `booktabs` package, so you have to make sure that package is [loaded in the preamble](http://www.sphinx-doc.org/en/master/latex.html) with:\n",
460+
"\n",
461+
" \\usepackage{booktabs}\n",
462+
"\n",
463+
"In order to allow page breaks within tables, you should use:"
464+
]
465+
},
466+
{
467+
"cell_type": "code",
468+
"execution_count": null,
469+
"metadata": {},
470+
"outputs": [],
471+
"source": [
472+
"pd.set_option('display.latex.longtable', True)"
473+
]
474+
},
475+
{
476+
"cell_type": "markdown",
477+
"metadata": {},
478+
"source": [
479+
"The `longtable` package is already used by Sphinx,\n",
480+
"so you don't have to manually load it in the preamble.\n",
481+
"\n",
482+
"Finally, if you want to use LaTeX math expressions in your dataframe, you'll have to disable escaping:"
483+
]
484+
},
485+
{
486+
"cell_type": "code",
487+
"execution_count": null,
488+
"metadata": {},
489+
"outputs": [],
490+
"source": [
491+
"pd.set_option('display.latex.escape', False)"
492+
]
493+
},
494+
{
495+
"cell_type": "markdown",
496+
"metadata": {},
497+
"source": [
498+
"The above settings should have no influence on the HTML output, but the LaTeX output should now look nicer:"
499+
]
500+
},
501+
{
502+
"cell_type": "code",
503+
"execution_count": null,
504+
"metadata": {},
505+
"outputs": [],
506+
"source": [
507+
"df = pd.DataFrame(np.random.randint(0, 100, size=[10, 4]),\n",
508+
" columns=[r'$\\alpha$', r'$\\beta$', r'$\\gamma$', r'$\\delta$'])\n",
509+
"df"
510+
]
511+
},
405512
{
406513
"cell_type": "markdown",
407514
"metadata": {},

doc/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
}{
144144
\renewcommand{\ttdefault}{lmtt} % typewriter font from lmodern
145145
}
146+
\usepackage{booktabs} % for Pandas dataframes
146147
""",
147148
}
148149

doc/readthedocs-environment.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ dependencies:
66
- pandoc
77
- nbconvert!=5.4
88
- ipykernel
9+
- numpy
910
- matplotlib
11+
- pandas
1012
- sphinxcontrib-bibtex

doc/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
sphinx>=1.6
22
nbconvert!=5.4
33
ipykernel
4+
numpy
45
matplotlib
6+
pandas
57
sphinxcontrib-bibtex

0 commit comments

Comments
 (0)