Skip to content

Commit fa3511b

Browse files
jdrudolphjreback
authored andcommitted
add unit test for writing to already open file (#21696)
1 parent 96cbc80 commit fa3511b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

pandas/tests/io/formats/test_to_csv.py

+16
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,19 @@ def test_to_csv_stdout_file(self):
300300
output = sys.stdout.getvalue()
301301
assert output == expected_ascii
302302
assert not sys.stdout.closed
303+
304+
def test_to_csv_write_to_open_file(self):
305+
# GH 21696
306+
df = pd.DataFrame({'a': ['x', 'y', 'z']})
307+
expected = '''\
308+
manual header
309+
x
310+
y
311+
z
312+
'''
313+
with tm.ensure_clean('test.txt') as path:
314+
with open(path, 'w') as f:
315+
f.write('manual header\n')
316+
df.to_csv(f, header=None, index=None)
317+
with open(path, 'r') as f:
318+
assert f.read() == expected

0 commit comments

Comments
 (0)