Skip to content

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

Closed
@gregmagee

Description

@gregmagee
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    IO HTMLread_html, to_html, Styler.apply, Styler.applymapOutput-Formatting__repr__ of pandas objects, to_stringRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions