From dedfb2acbf75fa9c913b068e1f6cb542ba1fa8e5 Mon Sep 17 00:00:00 2001 From: rtkaleta Date: Sat, 28 Oct 2017 19:00:09 +0100 Subject: [PATCH 1/3] to_csv now working for string arrays using ascii, still broken for utf-8 --- pandas/tests/io/formats/test_to_csv.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index b82d9895ddcf5..cb655e6554ce6 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -197,7 +197,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 +223,11 @@ 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(self): + # GH 10813 + str_array = [{'names': ['foo', 'bar']}, {'names': ['baz', 'qux']}] + df = pd.DataFrame(str_array) + expected = ",names\n0,\"['foo', 'bar']\"\n1,\"['baz', 'qux']\"\n" + assert df.to_csv(encoding='ascii') == expected + assert df.to_csv(path_or_buf='temp.csv', encoding='utf-8') == expected From 18a158f7c0759ccb865be713f42114cbcd45eb73 Mon Sep 17 00:00:00 2001 From: rtkaleta Date: Sun, 29 Oct 2017 08:59:46 +0000 Subject: [PATCH 2/3] Make the expected outcome easier to read --- pandas/tests/io/formats/test_to_csv.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index cb655e6554ce6..df0db1c3dd15a 100644 --- a/pandas/tests/io/formats/test_to_csv.py +++ b/pandas/tests/io/formats/test_to_csv.py @@ -228,6 +228,22 @@ def test_to_csv_string_array(self): # GH 10813 str_array = [{'names': ['foo', 'bar']}, {'names': ['baz', 'qux']}] df = pd.DataFrame(str_array) - expected = ",names\n0,\"['foo', 'bar']\"\n1,\"['baz', 'qux']\"\n" - assert df.to_csv(encoding='ascii') == expected - assert df.to_csv(path_or_buf='temp.csv', encoding='utf-8') == expected + 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 + + 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 From 86a3a1f7312e9e3f7d97a3464f0d56fd9e36f9e8 Mon Sep 17 00:00:00 2001 From: rtkaleta Date: Sun, 5 Nov 2017 14:34:53 +0000 Subject: [PATCH 3/3] Address comment about xfailing the utf8 case --- pandas/tests/io/formats/test_to_csv.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py index df0db1c3dd15a..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 @@ -224,7 +225,7 @@ 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(self): + def test_to_csv_string_array_ascii(self): # GH 10813 str_array = [{'names': ['foo', 'bar']}, {'names': ['baz', 'qux']}] df = pd.DataFrame(str_array) @@ -238,6 +239,11 @@ def test_to_csv_string_array(self): 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']"