question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

to_latex with float_format doesn't work like expected

See original GitHub issue

Code Sample

import pandas as pd
df = pd.DataFrame([[0.19999]])
print(df.to_latex(float_format='%.3f'))
# returns:
# \begin{tabular}{lr}
# \toprule
# {} &   0 \\
# \midrule
# 0 & 0.2 \\
# \bottomrule
# \end{tabular}

Problem description

The float format doesn’t seem to be respected in the example above. In contrast:

'%.3f' % df.values[0][0]
# returns
# '0.200'

I would expect the number to be rendered as 0.200 instead of 0.2.

Issue #6052 is possibly related, but the problem description is different.

Expected Output

\begin{tabular}{lr}
\toprule
{} &   0 \\
\midrule
0 & 0.200 \\
\bottomrule
\end{tabular}

Output of pd.show_versions()

Using Anaconda Python 3.6.5 with recent pandas 0.23.1.
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-128-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.1
pytest: None
pip: 10.0.1
setuptools: 39.2.0
Cython: None
numpy: 1.14.5
scipy: None
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tomneepcommented, Jun 27, 2018

I think the problem is that trailing zeros are being “trimmed” away. Presuming float_format should override this behaviour then a potential fix is 79b5acb5929333f0d0cd602b5ac206b29cc589a6. If that sounds like the right logic then I’ll open a PR (when time permits).

0reactions
WillAydcommented, Jun 26, 2018

float_format accepts a string for the other to_... functions and it should here, so this is still a bug if anyone wants to work it

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use pandas dataframe na_rep and float_format when ...
Using both na_rep and float_format on a dataframe when exporting to latex doesn't work as expected. ... Instead of formatting the floats and ......
Read more >
Org-mode radio table orgtbl-to-latex :fmt wont accept float
I can work around it by producing additional formatted colums and skip the original, but that seems messy. – kidmose. Dec 20, 2016...
Read more >
Controlling LaTeX floats
One of the most frustrating features of LaTeX is its insistence on moving figures and tables (and other “floating matter”) to unexpected locations....
Read more >
IO tools (text, CSV, HDF5, …) — pandas 1.5.2 documentation
float_format default None, a function which takes a single (float) argument and returns a formatted string; to be applied to floats in the...
Read more >
Errors while processing latex files
Look for other things that may not have been closed, like environments, math, etc. If stumped, try the general tricks. (Fatal format file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found