From bdc0cfdb00ae5277d6638b3de01b0e49a8ff0845 Mon Sep 17 00:00:00 2001 From: saldanhad Date: Fri, 4 Oct 2024 15:51:09 +0530 Subject: [PATCH 1/2] nbsp for strings --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/io/formats/html.py | 2 ++ pandas/tests/io/formats/test_format.py | 21 +++++++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index a5b4560a47bc4..2f596ab5e9a4b 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -627,6 +627,7 @@ I/O - Bug in :meth:`DataFrame.to_stata` when writing :class:`DataFrame` and ``byteorder=`big```. (:issue:`58969`) - Bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`) - Bug in :meth:`HDFStore.get` was failing to save data of dtype datetime64[s] correctly (:issue:`59004`) +- Bug in :meth:`HTMLFormatter._write_cell_` which ignored the spacing between strings. (:issue:`59876`) - Bug in :meth:`read_csv` causing segmentation fault when ``encoding_errors`` is not a string. (:issue:`59059`) - Bug in :meth:`read_csv` raising ``TypeError`` when ``index_col`` is specified and ``na_values`` is a dict containing the key ``None``. (:issue:`57547`) - Bug in :meth:`read_csv` raising ``TypeError`` when ``nrows`` and ``iterator`` are specified without specifying a ``chunksize``. (:issue:`59079`) 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..0bfead1adde10 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 ], - ) + ) # test float formatting and nbsp in strings 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 From b8342774d539b9533aeffdea86b47331ed8573eb Mon Sep 17 00:00:00 2001 From: saldanhad Date: Sat, 5 Oct 2024 00:14:49 +0530 Subject: [PATCH 2/2] update changes post review --- doc/source/whatsnew/v3.0.0.rst | 1 - pandas/tests/io/formats/test_format.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 2f596ab5e9a4b..a5b4560a47bc4 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -627,7 +627,6 @@ I/O - Bug in :meth:`DataFrame.to_stata` when writing :class:`DataFrame` and ``byteorder=`big```. (:issue:`58969`) - Bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`) - Bug in :meth:`HDFStore.get` was failing to save data of dtype datetime64[s] correctly (:issue:`59004`) -- Bug in :meth:`HTMLFormatter._write_cell_` which ignored the spacing between strings. (:issue:`59876`) - Bug in :meth:`read_csv` causing segmentation fault when ``encoding_errors`` is not a string. (:issue:`59059`) - Bug in :meth:`read_csv` raising ``TypeError`` when ``index_col`` is specified and ``na_values`` is a dict containing the key ``None``. (:issue:`57547`) - Bug in :meth:`read_csv` raising ``TypeError`` when ``nrows`` and ``iterator`` are specified without specifying a ``chunksize``. (:issue:`59079`) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 0bfead1adde10..0dc16e1ebc723 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -388,7 +388,7 @@ def test_repr_min_rows(self): "foo foo    foo", ), # even no.of spaces ], - ) # test float formatting and nbsp in strings + ) def test_repr_float_formatting_html_output( self, data, format_option, expected_values ):