From abee072700da47ae1f048551afe2aa07830d7b55 Mon Sep 17 00:00:00 2001 From: gshiba Date: Tue, 21 Aug 2018 00:42:57 -0700 Subject: [PATCH 1/7] Fix justifcation; update tests --- pandas/io/formats/format.py | 2 -- pandas/tests/io/formats/test_format.py | 18 ++++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 1ff0613876838..f07ae9f89088a 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -650,8 +650,6 @@ def to_string(self): self._chk_truncate() strcols = self._to_str_columns() text = self.adj.adjoin(1, *strcols) - if not self.index: - text = text.replace('\n ', '\n').strip() self.buf.writelines(text) if self.should_show_dimensions: diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index ffbc978b92ba5..b9ff7fa54428a 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -1272,7 +1272,14 @@ def test_to_string_no_index(self): df = DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]}) df_s = df.to_string(index=False) - expected = "x y\n1 4\n2 5\n3 6" + expected = " x y\n 1 4\n 2 5\n 3 6" + + assert df_s == expected + + df = DataFrame({'x': [1, 2, -3], 'y': [4, 5, -6]}) + + df_s = df.to_string(index=False) + expected = " x y\n 1 4\n 2 5\n-3 -6" assert df_s == expected @@ -1280,7 +1287,14 @@ def test_to_string_line_width_no_index(self): df = DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]}) df_s = df.to_string(line_width=1, index=False) - expected = "x \\\n1 \n2 \n3 \n\ny \n4 \n5 \n6" + expected = " x \\\n 1 \n 2 \n 3 \n\n y \n 4 \n 5 \n 6 " + + assert df_s == expected + + 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 " assert df_s == expected From e90320e90a7a0dfa570914bc3b0648b1e729b3b5 Mon Sep 17 00:00:00 2001 From: gshiba Date: Tue, 21 Aug 2018 12:25:30 -0700 Subject: [PATCH 2/7] Update/add tests --- pandas/tests/io/formats/test_format.py | 48 ++++++++++++++++++++------ 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index b9ff7fa54428a..3522df0c17ffe 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -1269,19 +1269,40 @@ def test_to_string_specified_header(self): df.to_string(header=['X']) def test_to_string_no_index(self): - df = DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]}) - df_s = df.to_string(index=False) - expected = " x y\n 1 4\n 2 5\n 3 6" + dfs = [ - assert df_s == expected + # ints + DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]}), + DataFrame({'x': [11, 22, 33], 'y': [4, 5, 6]}), + DataFrame({'x': [11, 22, -33], 'y': [4, 5, 6]}), + DataFrame({'x': [11, 22, -33], 'y': [4, 5, -6]}), + DataFrame({'x': [11, 22, -33], 'y': [44, 55, -66]}), - df = DataFrame({'x': [1, 2, -3], 'y': [4, 5, -6]}) + # floats + DataFrame({'x': [0.1, 0.2, -0.3], 'y': [4, 5, 6]}), + DataFrame({'x': [0.1, 0.2, -0.3], 'y': [0.4, 0.5, 0.6]}), + DataFrame({'x': [0.1, 0.2, -0.3], 'y': [0.4, 0.5, -0.6]}), + ] - df_s = df.to_string(index=False) - expected = " x y\n 1 4\n 2 5\n-3 -6" + exs = [ - assert df_s == expected + # ints + " x y\n 1 4\n 2 5\n 3 6", + " x y\n 11 4\n 22 5\n 33 6", + " x y\n 11 4\n 22 5\n-33 6", + " x y\n 11 4\n 22 5\n-33 -6", + " x y\n 11 44\n 22 55\n-33 -66", + + # floats + " x y\n 0.1 4\n 0.2 5\n-0.3 6", + " x y\n 0.1 0.4\n 0.2 0.5\n-0.3 0.6", + " x y\n 0.1 0.4\n 0.2 0.5\n-0.3 -0.6", + ] + + for df, expected in zip(dfs, exs): + df_s = df.to_string(index=False) + assert df_s == expected def test_to_string_line_width_no_index(self): df = DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]}) @@ -1291,10 +1312,17 @@ def test_to_string_line_width_no_index(self): assert df_s == expected - df = DataFrame({'x': [1, 2, -3], 'y': [4, 5, -6]}) + 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 " + + 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 1 \n 2 \n-3 \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 From 5b4a89c881df3662bb6f75b9ed982a669c22983c Mon Sep 17 00:00:00 2001 From: gshiba Date: Sat, 25 Aug 2018 00:23:58 -0700 Subject: [PATCH 3/7] Fix Series too --- pandas/io/formats/format.py | 3 +-- pandas/tests/io/formats/test_format.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index f07ae9f89088a..db86409adc2b0 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -288,8 +288,7 @@ def to_string(self): if self.index: result = self.adj.adjoin(3, *[fmt_index[1:], fmt_values]) else: - result = self.adj.adjoin(3, fmt_values).replace('\n ', - '\n').strip() + result = self.adj.adjoin(3, fmt_values) if self.header and have_header: result = fmt_index[0] + '\n' + result diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 3522df0c17ffe..c684b9569abd6 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -1835,7 +1835,7 @@ def test_to_string_without_index(self): # GH 11729 Test index=False option s = Series([1, 2, 3, 4]) result = s.to_string(index=False) - expected = (u('1\n') + '2\n' + '3\n' + '4') + expected = (u(' 1\n') + ' 2\n' + ' 3\n' + ' 4') assert result == expected def test_unicode_name_in_footer(self): From c539f24f0a65b2673d7d136da307d87dfb072f69 Mon Sep 17 00:00:00 2001 From: gshiba Date: Sat, 25 Aug 2018 14:53:49 -0700 Subject: [PATCH 4/7] Match style to other tests in this module --- pandas/tests/io/formats/test_format.py | 47 ++++++++------------------ 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index c684b9569abd6..5379567381bff 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -1270,39 +1270,20 @@ def test_to_string_specified_header(self): def test_to_string_no_index(self): - dfs = [ - - # ints - DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]}), - DataFrame({'x': [11, 22, 33], 'y': [4, 5, 6]}), - DataFrame({'x': [11, 22, -33], 'y': [4, 5, 6]}), - DataFrame({'x': [11, 22, -33], 'y': [4, 5, -6]}), - DataFrame({'x': [11, 22, -33], 'y': [44, 55, -66]}), - - # floats - DataFrame({'x': [0.1, 0.2, -0.3], 'y': [4, 5, 6]}), - DataFrame({'x': [0.1, 0.2, -0.3], 'y': [0.4, 0.5, 0.6]}), - DataFrame({'x': [0.1, 0.2, -0.3], 'y': [0.4, 0.5, -0.6]}), - ] - - exs = [ - - # ints - " x y\n 1 4\n 2 5\n 3 6", - " x y\n 11 4\n 22 5\n 33 6", - " x y\n 11 4\n 22 5\n-33 6", - " x y\n 11 4\n 22 5\n-33 -6", - " x y\n 11 44\n 22 55\n-33 -66", - - # floats - " x y\n 0.1 4\n 0.2 5\n-0.3 6", - " x y\n 0.1 0.4\n 0.2 0.5\n-0.3 0.6", - " x y\n 0.1 0.4\n 0.2 0.5\n-0.3 -0.6", - ] - - for df, expected in zip(dfs, exs): - df_s = df.to_string(index=False) - assert df_s == expected + df = DataFrame({'x': [11, 22], 'y': [33, -44], 'z': ['AAA', ' ']}) + + df_s = df.to_string(index=False) + # Leading space is expected for positive numbers. + expected = (" x y z\n + " 11 33 AAA\n" + " 22 -44 ") + assert df_s == expected + + df_s = df[['y', 'x', 'z']].to_string(index=False) + expected = (" y x z\n" + " 33 11 AAA\n" + "-44 22 ") + assert df_s == expected def test_to_string_line_width_no_index(self): df = DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]}) From cc1e14cc3b0ab1f7f78fe272f550a09ec69a79b2 Mon Sep 17 00:00:00 2001 From: gshiba Date: Sat, 25 Aug 2018 15:02:31 -0700 Subject: [PATCH 5/7] Add to What's new --- doc/source/whatsnew/v0.23.5.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/source/whatsnew/v0.23.5.txt b/doc/source/whatsnew/v0.23.5.txt index 8f4b1a13c2e9d..83a30e9f27fcf 100644 --- a/doc/source/whatsnew/v0.23.5.txt +++ b/doc/source/whatsnew/v0.23.5.txt @@ -52,3 +52,5 @@ Bug Fixes **I/O** - Bug in :func:`read_csv` that caused it to raise ``OverflowError`` when trying to use 'inf' as ``na_value`` with integer index column (:issue:`17128`) +- Bug in :func:`to_string(index=False)` that broke column alignment (:issue:`16839`, :issue:`13032`) +- From 8e96e64ee980816ca9e6237030bd568c83f880e2 Mon Sep 17 00:00:00 2001 From: gshiba Date: Sat, 25 Aug 2018 15:04:32 -0700 Subject: [PATCH 6/7] Fix typo --- pandas/tests/io/formats/test_format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index 5379567381bff..f0712548e4701 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -1274,7 +1274,7 @@ def test_to_string_no_index(self): df_s = df.to_string(index=False) # Leading space is expected for positive numbers. - expected = (" x y z\n + expected = (" x y z\n" " 11 33 AAA\n" " 22 -44 ") assert df_s == expected From cc86cd7e2a0a593f707675d3b9b3f801a4a9faf4 Mon Sep 17 00:00:00 2001 From: gshiba Date: Sun, 23 Sep 2018 21:32:11 -0700 Subject: [PATCH 7/7] Complete change requests --- doc/source/whatsnew/v0.23.5.txt | 2 -- doc/source/whatsnew/v0.24.0.txt | 1 + pandas/tests/io/formats/test_format.py | 3 ++- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.23.5.txt b/doc/source/whatsnew/v0.23.5.txt index 83a30e9f27fcf..8f4b1a13c2e9d 100644 --- a/doc/source/whatsnew/v0.23.5.txt +++ b/doc/source/whatsnew/v0.23.5.txt @@ -52,5 +52,3 @@ Bug Fixes **I/O** - Bug in :func:`read_csv` that caused it to raise ``OverflowError`` when trying to use 'inf' as ``na_value`` with integer index column (:issue:`17128`) -- Bug in :func:`to_string(index=False)` that broke column alignment (:issue:`16839`, :issue:`13032`) -- diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 6c91b6374b8af..c067adc8936a2 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -762,6 +762,7 @@ I/O - :func:`read_sas()` will correctly parse sas7bdat files with many columns (:issue:`22628`) - :func:`read_sas()` will correctly parse sas7bdat files with data page types having also bit 7 set (so page type is 128 + 256 = 384) (:issue:`16615`) - Bug in :meth:`detect_client_encoding` where potential ``IOError`` goes unhandled when importing in a mod_wsgi process due to restricted access to stdout. (:issue:`21552`) +- Bug in :func:`to_string()` that broke column alignment when ``index=False`` and width of first column's values is greater than the width of first column's header (:issue:`16839`, :issue:`13032`) Plotting ^^^^^^^^ diff --git a/pandas/tests/io/formats/test_format.py b/pandas/tests/io/formats/test_format.py index f0712548e4701..03e830fb09ad6 100644 --- a/pandas/tests/io/formats/test_format.py +++ b/pandas/tests/io/formats/test_format.py @@ -1269,7 +1269,7 @@ def test_to_string_specified_header(self): df.to_string(header=['X']) def test_to_string_no_index(self): - + # GH 16839, GH 13032 df = DataFrame({'x': [11, 22], 'y': [33, -44], 'z': ['AAA', ' ']}) df_s = df.to_string(index=False) @@ -1286,6 +1286,7 @@ def test_to_string_no_index(self): assert df_s == expected def test_to_string_line_width_no_index(self): + # GH 13998, GH 22505 df = DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]}) df_s = df.to_string(line_width=1, index=False)