Skip to content

Commit 139def2

Browse files
authored
BUG: fix html float display (#59930)
* 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
1 parent c47296a commit 139def2

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ I/O
620620
^^^
621621
- Bug in :class:`DataFrame` and :class:`Series` ``repr`` of :py:class:`collections.abc.Mapping`` elements. (:issue:`57915`)
622622
- 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`)
623+
- Bug in :meth:`DataFrame._repr_html_` which ignored the ``"display.float_format"`` option (:issue:`59876`)
623624
- Bug in :meth:`DataFrame.from_records` where ``columns`` parameter with numpy structured array was not reordering and filtering out the columns (:issue:`59717`)
624625
- Bug in :meth:`DataFrame.to_dict` raises unnecessary ``UserWarning`` when columns are not unique and ``orient='tight'``. (:issue:`58281`)
625626
- Bug in :meth:`DataFrame.to_excel` when writing empty :class:`DataFrame` with :class:`MultiIndex` on both axes (:issue:`57696`)

pandas/core/frame.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1192,14 +1192,15 @@ def _repr_html_(self) -> str | None:
11921192
min_rows = get_option("display.min_rows")
11931193
max_cols = get_option("display.max_columns")
11941194
show_dimensions = get_option("display.show_dimensions")
1195+
show_floats = get_option("display.float_format")
11951196

11961197
formatter = fmt.DataFrameFormatter(
11971198
self,
11981199
columns=None,
11991200
col_space=None,
12001201
na_rep="NaN",
12011202
formatters=None,
1202-
float_format=None,
1203+
float_format=show_floats,
12031204
sparsify=None,
12041205
justify=None,
12051206
index_names=True,

pandas/tests/io/formats/test_format.py

+17
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,23 @@ def test_repr_min_rows(self):
368368
assert ".." not in repr(df)
369369
assert ".." not in df._repr_html_()
370370

371+
@pytest.mark.parametrize(
372+
"data, format_option, expected_values",
373+
[
374+
(12345.6789, "{:12.3f}", "12345.679"),
375+
(None, "{:.3f}", "None"),
376+
("", "{:.2f}", ""),
377+
(112345.6789, "{:6.3f}", "112345.679"),
378+
],
379+
)
380+
def test_repr_float_formatting_html_output(
381+
self, data, format_option, expected_values
382+
):
383+
with option_context("display.float_format", format_option.format):
384+
df = DataFrame({"A": [data]})
385+
html_output = df._repr_html_()
386+
assert expected_values in html_output
387+
371388
def test_str_max_colwidth(self):
372389
# GH 7856
373390
df = DataFrame(

0 commit comments

Comments
 (0)