Skip to content

BUG: DataFrame does not display as LaTeX in Jupyter when it should #39911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task
AllenDowney opened this issue Feb 19, 2021 · 5 comments
Closed
1 task

BUG: DataFrame does not display as LaTeX in Jupyter when it should #39911

AllenDowney opened this issue Feb 19, 2021 · 5 comments
Labels
Closing Candidate May be closeable, needs more eyeballs IO LaTeX to_latex Styler conditional formatting using DataFrame.style

Comments

@AllenDowney
Copy link
Contributor

  • [ X] I have checked that this issue has not already been reported.

  • [ X] I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


If you run the following in a Jupyter cell:

import pandas as pd

pd.options.display.latex.repr = True

d = {'one' : [1., 2., 3., 4.],
     'two' : [4., 3., 2., 1.]}
df = pd.DataFrame(d)
df

The result is the HTML repr, as usual. But if you select a column, the Series shows the LaTeX repr, as it should.

df['one']

But DataFrame has _repr_latex_, because this works:

df._repr_latex_()

So it seems like DataFrame is ignoring the display.latex.repr option.

Problem description

When display.latex.repr is True, DataFrames should get represented in LaTeX, not HTML.

Expected Output

'\\begin{tabular}{lrr}\n\\toprule\n{} &  one &  two \\\\\n\\midrule\n0 &  1.0 &  4.0 \\\\\n1 &  2.0 &  3.0 \\\\\n2 &  3.0 &  2.0 \\\\\n3 &  4.0 &  1.0 \\\\\n\\bottomrule\n\\end{tabular}\n'

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : 7d32926 python : 3.7.9.final.0 python-bits : 64 OS : Linux OS-release : 4.15.0-72-generic Version : #81~16.04.1-Ubuntu SMP Tue Nov 26 16:34:21 UTC 2019 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.2.2
numpy : 1.19.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0.post20210125
Cython : None
pytest : 6.2.2
hypothesis : None
sphinx : 3.4.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.2
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.20.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.4
numexpr : 2.7.2
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : 1.3.23
tables : 3.6.1
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@AllenDowney AllenDowney added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 19, 2021
@AllenDowney
Copy link
Contributor Author

In case anyone else runs into this problem, I am currently working around it with this:

pd.DataFrame._repr_html_ = pd.DataFrame._repr_latex_

If it's stupid and it works...

@jbrockmendel jbrockmendel added IO LaTeX to_latex and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 24, 2021
@attack68 attack68 added the Styler conditional formatting using DataFrame.style label May 9, 2021
@westurner
Copy link
Contributor

westurner commented Aug 28, 2021

Should Pandas instead return a mime bundle with one or more representations of the table?

  • ENH: return mime bundles for Jupyter: text/plain, text/html, text/latex
    • Index?
    • Series
    • DataFrame
    • Panel
  • CFG: configurable list of formats to include in mime bundles for Jupyter

From https://news.ycombinator.com/item?id=25923123 :

A display_data Jupyter kernel message includes a data key with a dict value: "The data dict contains key/value pairs, where the keys are MIME types and the values are the raw data of the representation in that format." https://jupyter-client.readthedocs.io/en/latest/messaging.html#display-data

This looks like it does something with MIME bundles: https://github.com/jupyter-xeus/xeus-cling/blob/00b1fa69d17b390b7d3b470a9859281350863d30/src/xmime_internal.hpp#L173

ipython.display: https://github.com/ipython/ipython/blob/master/IPython/display.py

ipython.core.display: https://github.com/ipython/ipython/blob/master/IPython/core/display.py

ipython.lib.display: https://github.com/ipython/ipython/blob/master/IPython/lib/display.py

@westurner
Copy link
Contributor

https://ipython.readthedocs.io/en/stable/api/generated/IPython.core.formatters.html?highlight=text%2Flatex#IPython.core.formatters.DisplayFormatter :

Possible MIME types:

text/plain
text/html
text/markdown
text/latex
application/json
application/javascript
application/pdf
image/png
image/jpeg
image/svg+xml

Which of these formats does pandas produce now?

  • text/plain -- __str__()
  • text/html -- _repr_html_()
  • text/markdown
  • text/latex -- _repr_latex_()
  • application/json
  • application/ld+json
  • application/pdf

@attack68
Copy link
Contributor

attack68 commented Sep 4, 2021

I looked into MIME bundles recently as part of the PR above. One concern was performance. If Styler is processing a large DataFrame which may take a few seconds for HTML this may take longer for LaTeX and more for other types.
The implementation above yield either repr_html or repr_latex depending on the pandas option.

@attack68 attack68 added Closing Candidate May be closeable, needs more eyeballs and removed Bug labels Oct 26, 2021
@attack68
Copy link
Contributor

We are working toward implementing the Styler version of to_latex by default which doesn't have the problem you addressed after the above merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closing Candidate May be closeable, needs more eyeballs IO LaTeX to_latex Styler conditional formatting using DataFrame.style
Projects
None yet
Development

No branches or pull requests

4 participants