Skip to content

Commit

Permalink
BUG: fix html float display (#59930)
Browse files Browse the repository at this point in the history
* fix html display float/strings

* add test under io, update whatsnew

* fix linting

* changes to fix floats only

* Revert "fix linting"

This reverts commit 1061442.

* test script for float format

* remove nbsp implementation, keep floats

* Trigger CI

* implement changes post review

* lint check

* update test_formats.py

* rfc test_format.py

* update test cases
  • Loading branch information
saldanhad authored Oct 3, 2024
1 parent c47296a commit 139def2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ I/O
^^^
- Bug in :class:`DataFrame` and :class:`Series` ``repr`` of :py:class:`collections.abc.Mapping`` elements. (:issue:`57915`)
- Bug in :meth:`.DataFrame.to_json` when ``"index"`` was a value in the :attr:`DataFrame.column` and :attr:`Index.name` was ``None``. Now, this will fail with a ``ValueError`` (:issue:`58925`)
- Bug in :meth:`DataFrame._repr_html_` which ignored the ``"display.float_format"`` option (:issue:`59876`)
- Bug in :meth:`DataFrame.from_records` where ``columns`` parameter with numpy structured array was not reordering and filtering out the columns (:issue:`59717`)
- Bug in :meth:`DataFrame.to_dict` raises unnecessary ``UserWarning`` when columns are not unique and ``orient='tight'``. (:issue:`58281`)
- Bug in :meth:`DataFrame.to_excel` when writing empty :class:`DataFrame` with :class:`MultiIndex` on both axes (:issue:`57696`)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,14 +1192,15 @@ def _repr_html_(self) -> str | None:
min_rows = get_option("display.min_rows")
max_cols = get_option("display.max_columns")
show_dimensions = get_option("display.show_dimensions")
show_floats = get_option("display.float_format")

formatter = fmt.DataFrameFormatter(
self,
columns=None,
col_space=None,
na_rep="NaN",
formatters=None,
float_format=None,
float_format=show_floats,
sparsify=None,
justify=None,
index_names=True,
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,23 @@ def test_repr_min_rows(self):
assert ".." not in repr(df)
assert ".." not in df._repr_html_()

@pytest.mark.parametrize(
"data, format_option, expected_values",
[
(12345.6789, "{:12.3f}", "12345.679"),
(None, "{:.3f}", "None"),
("", "{:.2f}", ""),
(112345.6789, "{:6.3f}", "112345.679"),
],
)
def test_repr_float_formatting_html_output(
self, data, format_option, expected_values
):
with option_context("display.float_format", format_option.format):
df = DataFrame({"A": [data]})
html_output = df._repr_html_()
assert expected_values in html_output

def test_str_max_colwidth(self):
# GH 7856
df = DataFrame(
Expand Down

0 comments on commit 139def2

Please sign in to comment.