diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index 36c4ae547ad4e..20dfeb10daf46 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -300,3 +300,19 @@ 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): + # GH 21696 + 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