Skip to content

Commit 833ca69

Browse files
committed
fix html display float/strings
1 parent 90c26ce commit 833ca69

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

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/io/formats/html.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def _write_cell(
190190

191191
if self.escape:
192192
# escape & first to prevent double escaping of &
193-
esc = {"&": r"&amp;", "<": r"&lt;", ">": r"&gt;"}
193+
esc = {"&": r"&amp;", "<": r"&lt;", ">": r"&gt;"," ":"&nbsp;"}
194194
else:
195195
esc = {}
196196

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
import pandas as pd
3+
4+
5+
class Testfloatformat:
6+
@pytest.mark.parametrize("data, format_option, expected_values", [
7+
({"A": [12345.6789]}, "{:12.3f}", "&nbsp;&nbsp;&nbsp;12345.679"),
8+
({"A": [None]}, "{:.3f}", "&nbsp;None"),
9+
({"A": [""]}, "{:.2f}", "&nbsp;"),
10+
({"A": [112345.6789]}, "{:6.3f}", "112345.679"),
11+
({"A": ["foo foo"]}, None, "&nbsp;foo&nbsp;&nbsp;&nbsp;&nbsp;foo"),
12+
({"A": [None]}, None, "&nbsp;None"),
13+
({"A": ["foo foo foo"]}, None, "&nbsp;foo&nbsp;foo&nbsp;foo"),
14+
]) #test cases
15+
16+
def test_float_formatting_html_output(self,data,format_option, expected_values):
17+
# set float format, avoid for string checks
18+
if format_option is not None:
19+
pd.set_option("display.float_format", format_option.format)
20+
21+
# crate dataframe
22+
df = pd.DataFrame(data)
23+
24+
# capture html output
25+
html_output = df._repr_html_()
26+
27+
# check
28+
assert expected_values in html_output
29+
30+
# reset option
31+
if format_option is not None:
32+
pd.reset_option("display.float_format")

0 commit comments

Comments
 (0)