Skip to content
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

to_latex with float_format doesn't work like expected #21625

Closed
fzalkow opened this issue Jun 25, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@fzalkow
Copy link

commented Jun 25, 2018

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

@WillAyd

This comment has been minimized.

Copy link
Member

commented Jun 25, 2018

Thanks for the report - investigation and PRs are always welcome!

@tomneep

This comment has been minimized.

Copy link
Contributor

commented Jun 26, 2018

float_format takes a function rather than a string so if you do:

import pandas as pd
df = pd.DataFrame([[0.19999]])
print(df.to_latex(float_format=lambda x: '%.3f' % x))

you'll get what you want. It isn't just to_latex that is affected by this, to_string and to_html have the same behaviour. One problem is that the to_latex docstring isn't inheriting the docstring from to_string (which it probably should), so this information is hidden away.

to_csv on the otherhand, does seem to accept a string, according to the docstring.

@fzalkow

This comment has been minimized.

Copy link
Author

commented Jun 26, 2018

Oops, I wasn't aware it is expecting a function! Thanks a lot, this way it works! From my perspective, issue can be closed... Thanks again!

@WillAyd

This comment has been minimized.

Copy link
Member

commented 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

@tomneep

This comment has been minimized.

Copy link
Contributor

commented 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 79b5acb. If that sounds like the right logic then I'll open a PR (when time permits).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.