diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index b82d9895ddcf5..e12a7196dce6b 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- -from pandas import DataFrame import numpy as np import pandas as pd +import pytest +from pandas import DataFrame from pandas.util import testing as tm @@ -197,7 +198,7 @@ def test_to_csv_date_format(self): expected_ymd_sec) def test_to_csv_multi_index(self): - # see gh-6618 + # GH 6618 df = DataFrame([1], columns=pd.MultiIndex.from_arrays([[1], [2]])) exp = ",1\n,2\n0,1\n" @@ -223,3 +224,32 @@ def test_to_csv_multi_index(self): exp = "foo\nbar\n1\n" assert df.to_csv(index=False) == exp + + def test_to_csv_string_array_ascii(self): + # GH 10813 + str_array = [{'names': ['foo', 'bar']}, {'names': ['baz', 'qux']}] + df = pd.DataFrame(str_array) + expected_ascii = '''\ +,names +0,"['foo', 'bar']" +1,"['baz', 'qux']" +''' + with tm.ensure_clean('str_test.csv') as path: + df.to_csv(path, encoding='ascii') + with open(path, 'r') as f: + assert f.read() == expected_ascii + + @pytest.mark.xfail + def test_to_csv_string_array_utf8(self): + # GH 10813 + str_array = [{'names': ['foo', 'bar']}, {'names': ['baz', 'qux']}] + df = pd.DataFrame(str_array) + expected_utf8 = '''\ +,names +0,"[u'foo', u'bar']" +1,"[u'baz', u'qux']" +''' + with tm.ensure_clean('unicode_test.csv') as path: + df.to_csv(path, encoding='utf-8') + with open(path, 'r') as f: + assert f.read() == expected_utf8