Skip to content

add test case when to_csv argument is sys.stdout #21572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 22, 2018
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion pandas/tests/io/formats/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from pandas import DataFrame
from pandas.util import testing as tm

from pandas.util.testing import capture_stdout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be possible to use tm.capture_stdout rather add another import?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point!


class TestToCSV(object):

Expand Down Expand Up @@ -285,3 +285,18 @@ 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

@capture_stdout
def test_to_csv_stdout_file(self):
# GH 21561
df = pd.DataFrame([['foo', 'bar'], ['baz', 'qux']],
columns=['name_1', 'name_2'])
expected_ascii = '''\
,name_1,name_2
0,foo,bar
1,baz,qux
'''
df.to_csv(sys.stdout, encoding='ascii')
output = sys.stdout.getvalue()
assert output == expected_ascii
assert not sys.stdout.closed