Skip to content

Commit 2d2f6aa

Browse files
r00tajorisvandenbossche
authored andcommitted
add test case when to_csv argument is sys.stdout (#21572)
(cherry picked from commit 66fea91)
1 parent 4b1a687 commit 2d2f6aa

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

pandas/tests/io/formats/test_to_csv.py

+15
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,18 @@ def test_to_csv_string_array_utf8(self):
285285
df.to_csv(path, encoding='utf-8')
286286
with open(path, 'r') as f:
287287
assert f.read() == expected_utf8
288+
289+
@tm.capture_stdout
290+
def test_to_csv_stdout_file(self):
291+
# GH 21561
292+
df = pd.DataFrame([['foo', 'bar'], ['baz', 'qux']],
293+
columns=['name_1', 'name_2'])
294+
expected_ascii = '''\
295+
,name_1,name_2
296+
0,foo,bar
297+
1,baz,qux
298+
'''
299+
df.to_csv(sys.stdout, encoding='ascii')
300+
output = sys.stdout.getvalue()
301+
assert output == expected_ascii
302+
assert not sys.stdout.closed

0 commit comments

Comments
 (0)