From d4b0eadac00e6e9de849a1741618942b3357b878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Mart=C3=AD?= Date: Mon, 6 Apr 2015 15:37:19 -0300 Subject: [PATCH 1/2] better pandas typesetting in ipython nbconvert --to latex Currently, when IPython notebooks are converted to latex, pandas `DataFrame`s are appear as raw (verbatim) text. One simple solution is to include a `_repl_latex_` function. I have tried this solution and works. See https://github.com/ipython/ipython/issues/8261 --- pandas/core/frame.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f700d4316842c..de06044785891 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1428,7 +1428,10 @@ def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None, if buf is None: return formatter.buf.getvalue() - + + def _repr_latex_(self): + return self.to_latex() + def info(self, verbose=None, buf=None, max_cols=None, memory_usage=None, null_counts=None): """ Concise summary of a DataFrame. From a875ae423caa7e28440f56ca9a9ba0f27760fd47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Marti=CC=81?= Date: Wed, 21 Oct 2015 05:04:48 -0200 Subject: [PATCH 2/2] adding comments and tests as requested --- doc/source/whatsnew/v0.16.1.txt | 1 + pandas/core/frame.py | 5 +++++ pandas/tests/test_frame.py | 8 ++++++++ 3 files changed, 14 insertions(+) diff --git a/doc/source/whatsnew/v0.16.1.txt b/doc/source/whatsnew/v0.16.1.txt index f691b0842f071..31c61e5c36203 100644 --- a/doc/source/whatsnew/v0.16.1.txt +++ b/doc/source/whatsnew/v0.16.1.txt @@ -21,6 +21,7 @@ Enhancements - ``DataFrame.mask()`` and ``Series.mask()`` now support same keywords as ``where`` (:issue:`8801`) +- Added support for rendering DataFrames correctly when converting Jupyter/IPython notebooks to LaTeX (:issue:`9821`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index de06044785891..8c6ce6ee2641f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1430,6 +1430,11 @@ def to_latex(self, buf=None, columns=None, col_space=None, colSpace=None, return formatter.buf.getvalue() def _repr_latex_(self): + """ + Used for rendering a DataFrame when converting a Jupyter/IPython notebook to LaTeX. + For other uses refer to the `to_latex` function. + See https://github.com/pydata/pandas/pull/9821 for aditional information. + """ return self.to_latex() def info(self, verbose=None, buf=None, max_cols=None, memory_usage=None, null_counts=None): diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 3e4c16f63035f..633765fc552e1 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -4752,6 +4752,14 @@ def test_repr(self): self.assertFalse("\t" in repr(df)) self.assertFalse("\r" in repr(df)) self.assertFalse("a\n" in repr(df)) + + def test_repr_latex(self): + # #9821 + + df = DataFrame({'A': [1., 2., 3.], + 'B': ['a', 'b', 'c']}, + index=np.arange(3)) + self.assertEqual(df._repl_latex_(), df.to_latex()) def test_repr_dimensions(self): df = DataFrame([[1, 2,], [3, 4]])