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 2 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
19 changes: 19 additions & 0 deletions pandas/tests/io/formats/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest
from pandas import DataFrame
from pandas.util import testing as tm
from pandas.compat import StringIO


class TestToCSV(object):
Expand Down Expand Up @@ -285,3 +286,21 @@ 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

def test_to_csv_stdout_file(self):
# GH 21561
str_array = [{'names': ['foo', 'bar']}, {'names': ['baz', 'qux']}]
df = pd.DataFrame(str_array)
Copy link
Contributor

Choose a reason for hiding this comment

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

might be simpler to:
DataFrame([['foo', 'bar'], ['baz', 'qux']], columns=['name_1', 'name_2'])

expected_ascii = '''\
,names
0,"['foo', 'bar']"
1,"['baz', 'qux']"
'''
saved_stdout = sys.stdout
out = StringIO()
sys.stdout = out
df.to_csv(sys.stdout, encoding='ascii')
output = out.getvalue()
assert output == expected_ascii
sys.stdout = saved_stdout
assert sys.stdout.closed is False
Copy link
Contributor

@minggli minggli Jun 21, 2018

Choose a reason for hiding this comment

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

I would do below instead:
assert not sys.stdout.closed