|
10 | 10 |
|
11 | 11 | from pandas import Series, DataFrame
|
12 | 12 |
|
13 |
| -from pandas.compat import StringIO, u |
| 13 | +from pandas.compat import StringIO, u, PY2 |
14 | 14 | from pandas.util.testing import (assert_series_equal, assert_almost_equal,
|
15 | 15 | assert_frame_equal, ensure_clean)
|
16 | 16 | import pandas.util.testing as tm
|
@@ -137,29 +137,35 @@ def test_to_csv_path_is_none(self):
|
137 | 137 | csv_str = s.to_csv(path=None)
|
138 | 138 | assert isinstance(csv_str, str)
|
139 | 139 |
|
140 |
| - def test_to_csv_compression(self, compression): |
141 |
| - |
142 |
| - s = Series([0.123456, 0.234567, 0.567567], index=['A', 'B', 'C'], |
143 |
| - name='X') |
| 140 | + @pytest.mark.parametrize('s, encoding', [ |
| 141 | + (Series([0.123456, 0.234567, 0.567567], index=['A', 'B', 'C'], |
| 142 | + name='X'), 'utf-8'), |
| 143 | + (Series(['abc', 'def', 'ghi'], name='X'), 'ascii'), |
| 144 | + (Series(["123", u"你好", u"世界"], name=u"中文"), 'gb2312'), |
| 145 | + (Series(["123", u"Γειά σου", u"Κόσμε"], name=u"Ελληνικά"), 'cp737') |
| 146 | + ]) |
| 147 | + def test_to_csv_compression(self, s, encoding, compression): |
144 | 148 |
|
145 | 149 | with ensure_clean() as filename:
|
146 | 150 |
|
147 |
| - s.to_csv(filename, compression=compression, header=True) |
| 151 | + s.to_csv(filename, compression=compression, encoding=encoding, |
| 152 | + header=True) |
148 | 153 |
|
149 | 154 | # test the round trip - to_csv -> read_csv
|
150 | 155 | rs = pd.read_csv(filename, compression=compression,
|
151 |
| - index_col=0, squeeze=True) |
| 156 | + encoding=encoding, index_col=0, squeeze=True) |
152 | 157 | assert_series_equal(s, rs)
|
153 | 158 |
|
154 | 159 | # explicitly ensure file was compressed
|
155 | 160 | with tm.decompress_file(filename, compression) as fh:
|
156 |
| - text = fh.read().decode('utf8') |
| 161 | + text = fh.read().decode(encoding) |
157 | 162 | assert s.name in text
|
158 | 163 |
|
159 | 164 | with tm.decompress_file(filename, compression) as fh:
|
160 | 165 | assert_series_equal(s, pd.read_csv(fh,
|
161 | 166 | index_col=0,
|
162 |
| - squeeze=True)) |
| 167 | + squeeze=True, |
| 168 | + encoding=encoding)) |
163 | 169 |
|
164 | 170 |
|
165 | 171 | class TestSeriesIO(TestData):
|
|
0 commit comments