From 25be5b1f0a36ba079cd159053a8c6209012bc81f Mon Sep 17 00:00:00 2001 From: Rithik Reddy Date: Sun, 23 Apr 2023 14:19:49 -0500 Subject: [PATCH 01/10] fixed string formatting bug --- pandas/io/formats/format.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 4e2da746e0803..1a0d76e409ac9 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1428,7 +1428,10 @@ def _format(x): fmt_values = [] for i, v in enumerate(vals): if not is_float_type[i] and leading_space or self.formatter is not None: - fmt_values.append(f" {_format(v)}") + if leading_space: + fmt_values.append(f" {_format(v)}") + else: + fmt_values.append(f"{_format(v)}") elif is_float_type[i]: fmt_values.append(float_format(v)) else: From e929f2ddab373e85c41431a38f76a6a9da36914d Mon Sep 17 00:00:00 2001 From: Rithik Reddy Date: Sun, 23 Apr 2023 14:26:11 -0500 Subject: [PATCH 02/10] updating doc --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 107b22953ff79..64c3278efaed2 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -243,7 +243,7 @@ Conversion Strings ^^^^^^^ -- +- Bug in DataFrame.to_string() creates extra space for string dtypes in pandas >=2.0 (:issue:`52690`) - Interval From 2f65b79f3a829fb0fdeb4f46fc84a4b674b0313d Mon Sep 17 00:00:00 2001 From: Rithik Reddy Date: Sun, 23 Apr 2023 17:07:15 -0500 Subject: [PATCH 03/10] add tests --- doc/source/whatsnew/v2.0.1.rst | 1 + doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/tests/io/formats/test_format.py | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index 9071d242e25b5..0e0b880a05483 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -23,6 +23,7 @@ Bug fixes - Fixed bug in :func:`merge` when merging with ``ArrowDtype`` one one and a NumPy dtype on the other side (:issue:`52406`) - Bug in :meth:`Series.describe` not returning :class:`ArrowDtype` with ``pyarrow.float64`` type with numeric data (:issue:`52427`) - Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`) +- Bug in DataFrame.to_string() creates extra space for string dtypes in pandas >=2.0 (:issue:`52690`) .. --------------------------------------------------------------------------- .. _whatsnew_201.other: diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 64c3278efaed2..107b22953ff79 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -243,7 +243,7 @@ Conversion Strings ^^^^^^^ -- Bug in DataFrame.to_string() creates extra space for string dtypes in pandas >=2.0 (:issue:`52690`) +- - Interval diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 175c2478808b9..367ba6f6b8f8e 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -2099,6 +2099,23 @@ def test_max_rows_fitted(self, length, min_rows, max_rows, expected): result = formatter.max_rows_fitted assert result == expected + def test_no_extra_space(self): + """Check that no extra space is given + + GH #52690 + + """ + # Expected Output + col1 = "TEST" + col2 = "PANDAS" + col3 = "to_string" + expected = f"{col1:<6s} {col2:<7s} {col3:<10s}" + # Testing + df = DataFrame([{"col1": "TEST", "col2": "PANDAS", "col3": "to_string"}]) + d = {"col1": "{:<6s}".format, "col2": "{:<7s}".format, "col3": "{:<10s}".format} + result = df.to_string(index=False, header=False, formatters=d) + assert result == expected + def gen_series_formatting(): s1 = Series(["a"] * 100) From 6231f1ac20ad5f49c63f68857e7fec759690eb0e Mon Sep 17 00:00:00 2001 From: Rithik Reddy Date: Sun, 23 Apr 2023 19:31:30 -0500 Subject: [PATCH 04/10] change whatsnew --- doc/source/whatsnew/v2.0.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index 588b8d7ff1ce9..71f9fe6df8f36 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -21,6 +21,7 @@ Fixed regressions - Fixed regression in :meth:`Series.describe` showing ``RuntimeWarning`` for extension dtype :class:`Series` with one element (:issue:`52515`) - Fixed regression in :meth:`SeriesGroupBy.agg` failing when grouping with categorical data, multiple groupings, ``as_index=False``, and a list of aggregations (:issue:`52760`) - Fixed regression when adding a new column to a :class:`DataFrame` when the :attr:`DataFrame.columns` was a :class:`RangeIndex` and the new key was hashable but not a scalar (:issue:`52652`) +- Regression with Bug in DataFrame.to_string() creates extra space for string dtypes in pandas >=2.0 (:issue:`52690`) .. --------------------------------------------------------------------------- .. _whatsnew_201.bug_fixes: @@ -47,7 +48,6 @@ Bug fixes - Bug in logical and comparison operations between :class:`ArrowDtype` and numpy masked types (e.g. ``"boolean"``) (:issue:`52625`) - Fixed bug in :func:`merge` when merging with ``ArrowDtype`` one one and a NumPy dtype on the other side (:issue:`52406`) - Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`) -- Bug in DataFrame.to_string() creates extra space for string dtypes in pandas >=2.0 (:issue:`52690`) .. --------------------------------------------------------------------------- .. _whatsnew_201.other: From 113af37aa1cec0c1f5b708100514cbfd65dff13e Mon Sep 17 00:00:00 2001 From: reddyrg1 <59897313+reddyrg1@users.noreply.github.com> Date: Mon, 24 Apr 2023 21:19:19 -0500 Subject: [PATCH 05/10] Update doc/source/whatsnew/v2.0.1.rst Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/whatsnew/v2.0.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index 8f023d9711c76..d7fdfb16ddc02 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -21,7 +21,7 @@ Fixed regressions - Fixed regression in :meth:`MultiIndex.isin` raising ``TypeError`` for ``Generator`` (:issue:`52568`) - Fixed regression in :meth:`Series.describe` showing ``RuntimeWarning`` for extension dtype :class:`Series` with one element (:issue:`52515`) - Fixed regression when adding a new column to a :class:`DataFrame` when the :attr:`DataFrame.columns` was a :class:`RangeIndex` and the new key was hashable but not a scalar (:issue:`52652`) -- Regression with Bug in DataFrame.to_string() creates extra space for string dtypes in pandas >=2.0 (:issue:`52690`) +- Fixed regression in :meth:`DataFrame.to_string` creates extra space for string dtypes (:issue:`52690`) .. --------------------------------------------------------------------------- .. _whatsnew_201.bug_fixes: From 1a17b681ca1c0a0342c6768faa9cfc26fd3f69af Mon Sep 17 00:00:00 2001 From: reddyrg1 <59897313+reddyrg1@users.noreply.github.com> Date: Mon, 24 Apr 2023 22:10:57 -0500 Subject: [PATCH 06/10] Update v2.0.1.rst --- doc/source/whatsnew/v2.0.1.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index d7fdfb16ddc02..12f78b78dadb6 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -21,7 +21,7 @@ Fixed regressions - Fixed regression in :meth:`MultiIndex.isin` raising ``TypeError`` for ``Generator`` (:issue:`52568`) - Fixed regression in :meth:`Series.describe` showing ``RuntimeWarning`` for extension dtype :class:`Series` with one element (:issue:`52515`) - Fixed regression when adding a new column to a :class:`DataFrame` when the :attr:`DataFrame.columns` was a :class:`RangeIndex` and the new key was hashable but not a scalar (:issue:`52652`) -- Fixed regression in :meth:`DataFrame.to_string` creates extra space for string dtypes (:issue:`52690`) +- Fixed regression when :meth:`DataFrame.to_string` creates extra space for string dtypes (:issue:`52690`) .. --------------------------------------------------------------------------- .. _whatsnew_201.bug_fixes: From 81212cdb878ca4dbcfd7b17766819fbe167c2895 Mon Sep 17 00:00:00 2001 From: reddyrg1 <59897313+reddyrg1@users.noreply.github.com> Date: Mon, 24 Apr 2023 22:13:04 -0500 Subject: [PATCH 07/10] Update v2.0.1.rst --- doc/source/whatsnew/v2.0.1.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index 12f78b78dadb6..2613d12e43400 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -21,7 +21,6 @@ Fixed regressions - Fixed regression in :meth:`MultiIndex.isin` raising ``TypeError`` for ``Generator`` (:issue:`52568`) - Fixed regression in :meth:`Series.describe` showing ``RuntimeWarning`` for extension dtype :class:`Series` with one element (:issue:`52515`) - Fixed regression when adding a new column to a :class:`DataFrame` when the :attr:`DataFrame.columns` was a :class:`RangeIndex` and the new key was hashable but not a scalar (:issue:`52652`) -- Fixed regression when :meth:`DataFrame.to_string` creates extra space for string dtypes (:issue:`52690`) .. --------------------------------------------------------------------------- .. _whatsnew_201.bug_fixes: From 1f0e4d80634e78a7f1522efaa42cb873f2e0d278 Mon Sep 17 00:00:00 2001 From: Rithik Reddy Date: Mon, 24 Apr 2023 22:18:42 -0500 Subject: [PATCH 08/10] change whatsnew --- doc/source/whatsnew/v2.0.2.rst | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 doc/source/whatsnew/v2.0.2.rst diff --git a/doc/source/whatsnew/v2.0.2.rst b/doc/source/whatsnew/v2.0.2.rst new file mode 100644 index 0000000000000..135a3b79f3878 --- /dev/null +++ b/doc/source/whatsnew/v2.0.2.rst @@ -0,0 +1,41 @@ +.. _whatsnew_202: + +What's new in 2.0.2 (May ..., 2023) +----------------------------------- + +These are the changes in pandas 2.0.2. See :ref:`release` for a full changelog +including other versions of pandas. + +{{ header }} + +.. --------------------------------------------------------------------------- +.. _whatsnew_202.regressions: + +Fixed regressions +~~~~~~~~~~~~~~~~~ +- Fixed regression when :meth:`DataFrame.to_string` creates extra space for string dtypes (:issue:`52690`) + + +.. --------------------------------------------------------------------------- +.. _whatsnew_202.bug_fixes: + +Bug fixes +~~~~~~~~~ +- Bug in :func:`api.interchange.from_dataframe` was returning :class:`DataFrame`'s of incorrect sizes when called on slices (:issue:`52824`) +- Bug in :func:`api.interchange.from_dataframe` was unnecessarily raising on bitmasks (:issue:`49888`) +- + +.. --------------------------------------------------------------------------- +.. _whatsnew_202.other: + +Other +~~~~~ +- + +.. --------------------------------------------------------------------------- +.. _whatsnew_202.contributors: + +Contributors +~~~~~~~~~~~~ + +.. contributors:: v2.0.1..v2.0.2|HEAD From 0a0d0fc55743fff7eb2c0f3539d212f2c3a04222 Mon Sep 17 00:00:00 2001 From: reddyrg1 <59897313+reddyrg1@users.noreply.github.com> Date: Tue, 2 May 2023 14:41:06 -0500 Subject: [PATCH 09/10] Update pandas/tests/io/formats/test_format.py Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- pandas/tests/io/formats/test_format.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 367ba6f6b8f8e..6d64490d7a7b7 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -2100,11 +2100,7 @@ def test_max_rows_fitted(self, length, min_rows, max_rows, expected): assert result == expected def test_no_extra_space(self): - """Check that no extra space is given - - GH #52690 - - """ + # GH 52690: Check that no extra space is given # Expected Output col1 = "TEST" col2 = "PANDAS" From d540f71ce5c5f7bddc05a22f386a611e6851224e Mon Sep 17 00:00:00 2001 From: yuanx749 Date: Mon, 26 Jun 2023 19:18:08 +0800 Subject: [PATCH 10/10] Fix bug with smaller change --- doc/source/whatsnew/v2.0.2.rst | 1 - doc/source/whatsnew/v2.0.3.rst | 1 + pandas/io/formats/format.py | 7 ++----- pandas/tests/io/formats/test_format.py | 2 -- 4 files changed, 3 insertions(+), 8 deletions(-) diff --git a/doc/source/whatsnew/v2.0.2.rst b/doc/source/whatsnew/v2.0.2.rst index 1aad2f0cb4743..559078d501a00 100644 --- a/doc/source/whatsnew/v2.0.2.rst +++ b/doc/source/whatsnew/v2.0.2.rst @@ -48,7 +48,6 @@ Other ~~~~~ - Raised a better error message when calling :func:`Series.dt.to_pydatetime` with :class:`ArrowDtype` with ``pyarrow.date32`` or ``pyarrow.date64`` type (:issue:`52812`) - .. --------------------------------------------------------------------------- .. _whatsnew_202.contributors: diff --git a/doc/source/whatsnew/v2.0.3.rst b/doc/source/whatsnew/v2.0.3.rst index 3e12af946e661..8aab1e7b58f5e 100644 --- a/doc/source/whatsnew/v2.0.3.rst +++ b/doc/source/whatsnew/v2.0.3.rst @@ -14,6 +14,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ - Fixed performance regression in merging on datetime-like columns (:issue:`53231`) +- Fixed regression when :meth:`DataFrame.to_string` creates extra space for string dtypes (:issue:`52690`) - For external ExtensionArray implementations, restored the default use of ``_values_for_factorize`` for hashing arrays (:issue:`53475`) - diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 3c85d0ba998b6..a9aeb5b9e0241 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1391,11 +1391,8 @@ def _format(x): fmt_values = [] for i, v in enumerate(vals): - if not is_float_type[i] and leading_space or self.formatter is not None: - if leading_space: - fmt_values.append(f" {_format(v)}") - else: - fmt_values.append(f"{_format(v)}") + if (not is_float_type[i] or self.formatter is not None) and leading_space: + fmt_values.append(f" {_format(v)}") elif is_float_type[i]: fmt_values.append(float_format(v)) else: diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index ab19939655498..92ced33ab338a 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -2208,12 +2208,10 @@ def test_max_rows_fitted(self, length, min_rows, max_rows, expected): def test_no_extra_space(self): # GH 52690: Check that no extra space is given - # Expected Output col1 = "TEST" col2 = "PANDAS" col3 = "to_string" expected = f"{col1:<6s} {col2:<7s} {col3:<10s}" - # Testing df = DataFrame([{"col1": "TEST", "col2": "PANDAS", "col3": "to_string"}]) d = {"col1": "{:<6s}".format, "col2": "{:<7s}".format, "col3": "{:<10s}".format} result = df.to_string(index=False, header=False, formatters=d)