From d46472b12f719d218f9e3fc84c30f7b98c09ff70 Mon Sep 17 00:00:00 2001 From: drewchen Date: Wed, 16 Nov 2022 19:58:44 -0500 Subject: [PATCH 1/4] BUG: GH49230 to_string(header=False) fix --- pandas/io/formats/printing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/printing.py b/pandas/io/formats/printing.py index 8091590e2e89d..0338cb018049b 100644 --- a/pandas/io/formats/printing.py +++ b/pandas/io/formats/printing.py @@ -51,7 +51,7 @@ def adjoin(space: int, *lists: list[str], **kwargs) -> str: maxLen = max(map(len, lists)) for i, lst in enumerate(lists): nl = justfunc(lst, lengths[i], mode="left") - nl.extend([" " * lengths[i]] * (maxLen - len(lst))) + nl = ([" " * lengths[i]] * (maxLen - len(lst))) + nl newLists.append(nl) toJoin = zip(*newLists) for lines in toJoin: From c53c23fce89d68f5d71579ed10775e91b692d8f1 Mon Sep 17 00:00:00 2001 From: drewchen Date: Wed, 16 Nov 2022 20:34:08 -0500 Subject: [PATCH 2/4] TST:Updated and Added Test Cases for GH49230 --- pandas/tests/io/formats/test_format.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index f870ef25991df..d7fd03e67c3d4 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -1414,25 +1414,25 @@ def test_to_string_no_index(self): assert df_s == expected def test_to_string_line_width_no_index(self): - # GH 13998, GH 22505 + # GH 13998, GH 22505, # GH 49230 df = DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]}) df_s = df.to_string(line_width=1, index=False) - expected = " x \\\n 1 \n 2 \n 3 \n\n y \n 4 \n 5 \n 6 " + expected = " x \n 1 \\\n 2 \n 3 \n\n y \n 4 \n 5 \n 6 " assert df_s == expected df = DataFrame({"x": [11, 22, 33], "y": [4, 5, 6]}) df_s = df.to_string(line_width=1, index=False) - expected = " x \\\n11 \n22 \n33 \n\n y \n 4 \n 5 \n 6 " + expected = " x \n11 \\\n22 \n33 \n\n y \n 4 \n 5 \n 6 " assert df_s == expected df = DataFrame({"x": [11, 22, -33], "y": [4, 5, -6]}) df_s = df.to_string(line_width=1, index=False) - expected = " x \\\n 11 \n 22 \n-33 \n\n y \n 4 \n 5 \n-6 " + expected = " x \n 11 \\\n 22 \n-33 \n\n y \n 4 \n 5 \n-6 " assert df_s == expected @@ -1686,6 +1686,20 @@ def test_to_string_line_width(self): s = df.to_string(line_width=80) assert max(len(line) for line in s.split("\n")) == 80 + def test_to_string_header_false(self): + # GH 49230 + df = DataFrame([1, 2]) + df.index.name = "a" + s = df.to_string(header=False) + expected = "a \n0 1\n1 2" + assert s == expected + + df = DataFrame([[1, 2], [3, 4]]) + df.index.name = "a" + s = df.to_string(header=False) + expected = "a \n0 1 2\n1 3 4" + assert s == expected + def test_show_dimensions(self): df = DataFrame(123, index=range(10, 15), columns=range(30)) From ef5de4046db890401867c9ef73cd38da645d6987 Mon Sep 17 00:00:00 2001 From: drewchen Date: Wed, 16 Nov 2022 20:49:29 -0500 Subject: [PATCH 3/4] DOC: Added release notes documentation --- doc/source/whatsnew/v2.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index df190a4df393c..fc5215fc5546d 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -551,6 +551,7 @@ I/O - Improved error message in :func:`read_excel` by including the offending sheet name when an exception is raised while reading a file (:issue:`48706`) - Bug when a pickling a subset PyArrow-backed data that would serialize the entire data instead of the subset (:issue:`42600`) - Bug in :func:`read_csv` for a single-line csv with fewer columns than ``names`` raised :class:`.errors.ParserError` with ``engine="c"`` (:issue:`47566`) +- Bug in :func:`adjoin` used for DataFrame.to_string(header=False) printed index.name on the same line as the first row of the data (:issue: ``49230``) - Period From f7a634479c240d5002772b146da3bccebe051766 Mon Sep 17 00:00:00 2001 From: drewchen Date: Thu, 17 Nov 2022 13:32:46 -0500 Subject: [PATCH 4/4] DOC:Updated release notes --- doc/source/whatsnew/v2.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index a32107aa1aecf..adc883957b414 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -692,7 +692,7 @@ I/O - Improved error message in :func:`read_excel` by including the offending sheet name when an exception is raised while reading a file (:issue:`48706`) - Bug when a pickling a subset PyArrow-backed data that would serialize the entire data instead of the subset (:issue:`42600`) - Bug in :func:`read_csv` for a single-line csv with fewer columns than ``names`` raised :class:`.errors.ParserError` with ``engine="c"`` (:issue:`47566`) -- Bug in :func:`adjoin` used for DataFrame.to_string(header=False) printed index.name on the same line as the first row of the data (:issue: ``49230``) +- Bug in :func:`DataFrame.to_string` with ``header=False`` that printed the index name on the same line as the first row of the data (:issue:`49230`) - Fixed memory leak which stemmed from the initialization of the internal JSON module (:issue:`49222`) -