From b773d7afeca549d48833bf28e930d54d8d05cc32 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 1 Jul 2018 12:28:41 +0200 Subject: [PATCH 1/2] add unit test for writing to already open file --- pandas/tests/io/formats/test_to_csv.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index 36c4ae547ad4e..dc7914097e10a 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -300,3 +300,18 @@ def test_to_csv_stdout_file(self): output = sys.stdout.getvalue() assert output == expected_ascii assert not sys.stdout.closed + + def test_to_csv_write_to_open_file(self): + df = pd.DataFrame({'a': ['x', 'y', 'z']}) + expected = '''\ +manual header +x +y +z +''' + with tm.ensure_clean('test.txt') as path: + with open(path, 'w') as f: + f.write('manual header\n') + df.to_csv(f, header=None, index=None) + with open(path, 'r') as f: + assert f.read() == expected From f018837f50d80c38865190252840ca46cf52eec6 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 1 Jul 2018 12:35:33 +0200 Subject: [PATCH 2/2] add GH pull request id --- pandas/tests/io/formats/test_to_csv.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index dc7914097e10a..20dfeb10daf46 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -302,6 +302,7 @@ def test_to_csv_stdout_file(self): assert not sys.stdout.closed def test_to_csv_write_to_open_file(self): + # GH 21696 df = pd.DataFrame({'a': ['x', 'y', 'z']}) expected = '''\ manual header