From ab04bc9d806df6e91cd8de2e74321609ddaa2af3 Mon Sep 17 00:00:00 2001 From: r00ta Date: Thu, 21 Jun 2018 11:27:49 +0000 Subject: [PATCH 1/7] add test case when to_csv argument is sys.stdout --- pandas/tests/io/formats/test_to_csv.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index dfa3751bff57a..6084400abb20d 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -6,6 +6,7 @@ import pytest from pandas import DataFrame from pandas.util import testing as tm +from pandas.compat import StringIO class TestToCSV(object): @@ -285,3 +286,21 @@ def test_to_csv_string_array_utf8(self): df.to_csv(path, encoding='utf-8') with open(path, 'r') as f: assert f.read() == expected_utf8 + + def test_to_csv_stdout_file(self): + # GH 21561 + str_array = [{'names': ['foo', 'bar']}, {'names': ['baz', 'qux']}] + df = pd.DataFrame(str_array) + expected_ascii = '''\ +,names +0,"['foo', 'bar']" +1,"['baz', 'qux']" +''' + saved_stdout = sys.stdout + out = StringIO() + sys.stdout = out + df.to_csv(sys.stdout, encoding='ascii') + output = out.getvalue() + assert output == expected_ascii + sys.stdout = saved_stdout + assert sys.stdout.closed == False From 96634c11986c4fba1dd44f5c47879648cddc908f Mon Sep 17 00:00:00 2001 From: r00ta Date: Thu, 21 Jun 2018 11:32:16 +0000 Subject: [PATCH 2/7] fix PEP8 issue comparison with False --- pandas/tests/io/formats/test_to_csv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index 6084400abb20d..cd3197bc35ed2 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -303,4 +303,4 @@ def test_to_csv_stdout_file(self): output = out.getvalue() assert output == expected_ascii sys.stdout = saved_stdout - assert sys.stdout.closed == False + assert sys.stdout.closed is False From 49b486a563b0ad6cafe71216ad147a3b4e400a94 Mon Sep 17 00:00:00 2001 From: r00ta Date: Thu, 21 Jun 2018 16:15:45 +0000 Subject: [PATCH 3/7] add to_csv test when argument is sys.stdout using the decorator capture_stdout --- pandas/tests/io/formats/test_to_csv.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index cd3197bc35ed2..ab37c8484d1f1 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -7,7 +7,7 @@ from pandas import DataFrame from pandas.util import testing as tm from pandas.compat import StringIO - +from pandas.util.testing import capture_stdout class TestToCSV(object): @@ -287,20 +287,16 @@ def test_to_csv_string_array_utf8(self): with open(path, 'r') as f: assert f.read() == expected_utf8 + @capture_stdout def test_to_csv_stdout_file(self): # GH 21561 - str_array = [{'names': ['foo', 'bar']}, {'names': ['baz', 'qux']}] - df = pd.DataFrame(str_array) + df = pd.DataFrame([['foo', 'bar'], ['baz', 'qux']], columns=['name_1', 'name_2']) expected_ascii = '''\ -,names -0,"['foo', 'bar']" -1,"['baz', 'qux']" +,name_1,name_2 +0,foo,bar +1,baz,qux ''' - saved_stdout = sys.stdout - out = StringIO() - sys.stdout = out df.to_csv(sys.stdout, encoding='ascii') - output = out.getvalue() + output = sys.stdout.getvalue() assert output == expected_ascii - sys.stdout = saved_stdout - assert sys.stdout.closed is False + assert not sys.stdout.closed From 59f591983c124ef776c714801c54d31b950ebd3e Mon Sep 17 00:00:00 2001 From: r00ta Date: Fri, 22 Jun 2018 06:33:39 +0000 Subject: [PATCH 4/7] remove unused import --- pandas/tests/io/formats/test_to_csv.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index ab37c8484d1f1..165e0ea874e4e 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -6,7 +6,6 @@ import pytest from pandas import DataFrame from pandas.util import testing as tm -from pandas.compat import StringIO from pandas.util.testing import capture_stdout class TestToCSV(object): From d61d79287b5f5c6b4bc6f636179b4944872bd891 Mon Sep 17 00:00:00 2001 From: Jacopo Date: Fri, 22 Jun 2018 12:23:04 +0200 Subject: [PATCH 5/7] fix lint error - line too long --- pandas/tests/io/formats/test_to_csv.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index 165e0ea874e4e..d68a19b72b16b 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -289,7 +289,8 @@ def test_to_csv_string_array_utf8(self): @capture_stdout def test_to_csv_stdout_file(self): # GH 21561 - df = pd.DataFrame([['foo', 'bar'], ['baz', 'qux']], columns=['name_1', 'name_2']) + df = pd.DataFrame([['foo', 'bar'], ['baz', 'qux']], + columns=['name_1', 'name_2']) expected_ascii = '''\ ,name_1,name_2 0,foo,bar From 557fa9fd252db5c4aeb1bcfbf73ed6da4917decb Mon Sep 17 00:00:00 2001 From: r00ta Date: Fri, 22 Jun 2018 13:01:14 +0000 Subject: [PATCH 6/7] remove useless double import of testing module --- pandas/tests/io/formats/test_to_csv.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index d68a19b72b16b..a7882c04dc3d3 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -6,7 +6,6 @@ import pytest from pandas import DataFrame from pandas.util import testing as tm -from pandas.util.testing import capture_stdout class TestToCSV(object): @@ -286,7 +285,7 @@ def test_to_csv_string_array_utf8(self): with open(path, 'r') as f: assert f.read() == expected_utf8 - @capture_stdout + @tm.capture_stdout def test_to_csv_stdout_file(self): # GH 21561 df = pd.DataFrame([['foo', 'bar'], ['baz', 'qux']], From b25025904e6e21887bcb99043f588133f6203d66 Mon Sep 17 00:00:00 2001 From: Jacopo Date: Fri, 22 Jun 2018 17:23:51 +0200 Subject: [PATCH 7/7] fix according to PEP-8 standard --- pandas/tests/io/formats/test_to_csv.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index a7882c04dc3d3..36c4ae547ad4e 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -7,6 +7,7 @@ from pandas import DataFrame from pandas.util import testing as tm + class TestToCSV(object): @pytest.mark.xfail((3, 6, 5) > sys.version_info >= (3, 5), @@ -288,7 +289,7 @@ def test_to_csv_string_array_utf8(self): @tm.capture_stdout def test_to_csv_stdout_file(self): # GH 21561 - df = pd.DataFrame([['foo', 'bar'], ['baz', 'qux']], + df = pd.DataFrame([['foo', 'bar'], ['baz', 'qux']], columns=['name_1', 'name_2']) expected_ascii = '''\ ,name_1,name_2