Skip to content

BUG: Calling to_html with float_format strips all trailing zeros if an integer string is returned from the formatter #40024

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
2 of 3 tasks
gregmagee opened this issue Feb 24, 2021 · 2 comments · Fixed by #40850
Labels
IO HTML read_html, to_html, Styler.apply, Styler.applymap Output-Formatting __repr__ of pandas objects, to_string Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@gregmagee
Copy link

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

  • 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.


Code Sample, a copy-pastable example

import pandas as pd
df = pd.DataFrame(data={'x': [1000.0, 'test']}) # Column dtype must be object
df.to_html(float_format=lambda x: '{:,.0f}'.format(x))

Output:

'<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>x</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1,</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>test</td>\n    </tr>\n  </tbody>\n</table>'

Problem description

This caused by the rstrip in pandas.io.formats.format._trim_zeros_single_float()

Expected Output

'<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>x</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1,000</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>test</td>\n    </tr>\n  </tbody>\n</table>'

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
pandas           : 1.2.2
numpy            : 1.20.1
pytz             : 2021.1
dateutil         : 2.8.1
pip              : 20.3.3
setuptools       : 51.3.3
@gregmagee gregmagee added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 24, 2021
@attack68
Copy link
Contributor

not ideal but you can solve this with:

df.style.format(lambda x: f'{x:,.0f}' if isinstance(x, (float, int)) else '{x}').render()

there are few issues with .to_html()

@attack68 attack68 added IO HTML read_html, to_html, Styler.apply, Styler.applymap and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 24, 2021
@mzeitlin11
Copy link
Member

Looks like a regression caused by #38759, don't think specific to to_html

@mzeitlin11 mzeitlin11 added Output-Formatting __repr__ of pandas objects, to_string Regression Functionality that used to work in a prior pandas version and removed Bug labels Apr 9, 2021
@jreback jreback added this to the 1.2.4 milestone Apr 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO HTML read_html, to_html, Styler.apply, Styler.applymap Output-Formatting __repr__ of pandas objects, to_string Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants