Skip to content

Commit 2e93d21

Browse files
rtkaletawatercrossing
authored andcommitted
Fix df.to_csv() for string arrays when encoded in utf-8 (pandas-dev#18013)
1 parent f8c16f7 commit 2e93d21

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

pandas/tests/io/formats/test_to_csv.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# -*- coding: utf-8 -*-
22

3-
from pandas import DataFrame
43
import numpy as np
54
import pandas as pd
5+
import pytest
6+
from pandas import DataFrame
67
from pandas.util import testing as tm
78

89

@@ -197,7 +198,7 @@ def test_to_csv_date_format(self):
197198
expected_ymd_sec)
198199

199200
def test_to_csv_multi_index(self):
200-
# see gh-6618
201+
# GH 6618
201202
df = DataFrame([1], columns=pd.MultiIndex.from_arrays([[1], [2]]))
202203

203204
exp = ",1\n,2\n0,1\n"
@@ -223,3 +224,32 @@ def test_to_csv_multi_index(self):
223224

224225
exp = "foo\nbar\n1\n"
225226
assert df.to_csv(index=False) == exp
227+
228+
def test_to_csv_string_array_ascii(self):
229+
# GH 10813
230+
str_array = [{'names': ['foo', 'bar']}, {'names': ['baz', 'qux']}]
231+
df = pd.DataFrame(str_array)
232+
expected_ascii = '''\
233+
,names
234+
0,"['foo', 'bar']"
235+
1,"['baz', 'qux']"
236+
'''
237+
with tm.ensure_clean('str_test.csv') as path:
238+
df.to_csv(path, encoding='ascii')
239+
with open(path, 'r') as f:
240+
assert f.read() == expected_ascii
241+
242+
@pytest.mark.xfail
243+
def test_to_csv_string_array_utf8(self):
244+
# GH 10813
245+
str_array = [{'names': ['foo', 'bar']}, {'names': ['baz', 'qux']}]
246+
df = pd.DataFrame(str_array)
247+
expected_utf8 = '''\
248+
,names
249+
0,"[u'foo', u'bar']"
250+
1,"[u'baz', u'qux']"
251+
'''
252+
with tm.ensure_clean('unicode_test.csv') as path:
253+
df.to_csv(path, encoding='utf-8')
254+
with open(path, 'r') as f:
255+
assert f.read() == expected_utf8

0 commit comments

Comments
 (0)