Skip to content
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

BUG: fix html float display #59930

Merged
merged 19 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ 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_` pass :func: ``get_option("display.float_format")`` to :class:`DataFrameFormatter`, such that HTML output respects the configured float formatting. (:issue:`59876`)
saldanhad marked this conversation as resolved.
Show resolved Hide resolved
- Bug in :meth:`DataFrame._repr_html_` pass :func:`get_option("display.float_format")` to :class:`DataFrameFormatter`, such that HTML output respects the configured float formatting (:issue:`59876`)
saldanhad marked this conversation as resolved.
Show resolved Hide resolved
- 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
32 changes: 32 additions & 0 deletions pandas/tests/io/formats/test_info_repr_html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest

import pandas as pd


class Testfloatformat:
@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"),
],
) # test cases
def test_float_formatting_html_output(self, data, format_option, expected_values):
saldanhad marked this conversation as resolved.
Show resolved Hide resolved
# set float format, avoid for string checks
if format_option is not None:
pd.set_option("display.float_format", format_option.format)

# create dataframe
df = pd.DataFrame(data)

# capture html output
html_output = df._repr_html_()

# check
assert expected_values in html_output

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