diff --git a/pandas/io/formats/html.py b/pandas/io/formats/html.py
index adaeed017d7bf..fdea1831d5596 100644
--- a/pandas/io/formats/html.py
+++ b/pandas/io/formats/html.py
@@ -195,6 +195,8 @@ def _write_cell(
esc = {}
rs = pprint_thing(s, escape_chars=esc).strip()
+ # replace spaces betweens strings with non-breaking spaces
+ rs = rs.replace(" ", " ")
if self.render_links and is_url(rs):
rs_unescaped = pprint_thing(s, escape_chars={}).strip()
diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py
index 82cc3a838ca68..0dc16e1ebc723 100644
--- a/pandas/tests/io/formats/test_format.py
+++ b/pandas/tests/io/formats/test_format.py
@@ -375,12 +375,29 @@ def test_repr_min_rows(self):
(None, "{:.3f}", "None"),
("", "{:.2f}", ""),
(112345.6789, "{:6.3f}", "112345.679"),
+ ("foo foo", None, "foo foo"),
+ (" foo", None, "foo"),
+ (
+ "foo foo foo",
+ None,
+ "foo foo foo",
+ ), # odd no.of spaces
+ (
+ "foo foo foo",
+ None,
+ "foo foo foo",
+ ), # even no.of spaces
],
)
def test_repr_float_formatting_html_output(
self, data, format_option, expected_values
):
- with option_context("display.float_format", format_option.format):
+ if format_option is not None:
+ with option_context("display.float_format", format_option.format):
+ df = DataFrame({"A": [data]})
+ html_output = df._repr_html_()
+ assert expected_values in html_output
+ else:
df = DataFrame({"A": [data]})
html_output = df._repr_html_()
assert expected_values in html_output