Skip to content

BUG: fix html float display #59930

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

Merged
merged 19 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 24 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
option_context,
read_csv,
reset_option,
set_option,
)

from pandas.io.formats import printing
Expand Down Expand Up @@ -368,6 +369,29 @@ 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",
[
({"A": [12345.6789]}, "{:12.3f}", "12345.679"),
({"A": [None]}, "{:.3f}", "None"),
({"A": [""]}, "{:.2f}", ""),
({"A": [112345.6789]}, "{:6.3f}", "112345.679"),
],
)
def test_repr_float_formatting_html_output(
self, data, format_option, expected_values
):
if format_option is not None:
set_option("display.float_format", format_option.format)

df = DataFrame(data)
html_output = df._repr_html_()
assert expected_values in html_output

# reset option
if format_option is not None:
reset_option("display.float_format")

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