Skip to content

BUG: encoding error in to_csv compression #21300

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 19 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ I/O

- Bug in IO methods specifying ``compression='zip'`` which produced uncompressed zip archives (:issue:`17778`, :issue:`21144`)
- Bug in :meth:`DataFrame.to_stata` which prevented exporting DataFrames to buffers and most file-like objects (:issue:`21041`)
- Bug in :meth:`DataFrame.to_csv` using compression causes encoding error (:issue:`21241`)
Copy link
Member

Choose a reason for hiding this comment

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

Could also mention Series.to_csv here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added.

-

Plotting
Expand Down
7 changes: 5 additions & 2 deletions pandas/io/formats/csvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,14 @@ def save(self):
self._save()

finally:
# GH 17778 handles compression for byte strings.
# GH 17778 handles zip compression for byte strings separately to
# support Python 2, also allow compression file handle
if not close and self.compression:
f.close()
with open(f.name, 'r') as f:
with open(f.name, 'rb') as f:
data = f.read()
if not compat.PY2:
data = data.decode(encoding)
f, handles = _get_handle(f.name, self.mode,
encoding=encoding,
compression=self.compression)
Expand Down
30 changes: 19 additions & 11 deletions pandas/tests/frame/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,29 +919,37 @@ def test_to_csv_path_is_none(self):
recons = pd.read_csv(StringIO(csv_str), index_col=0)
assert_frame_equal(self.frame, recons)

def test_to_csv_compression(self, compression):

df = DataFrame([[0.123456, 0.234567, 0.567567],
[12.32112, 123123.2, 321321.2]],
index=['A', 'B'], columns=['X', 'Y', 'Z'])
@pytest.mark.parametrize('frame, encoding', [
Copy link
Member

Choose a reason for hiding this comment

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

Use df instead of frame and remove the whitespace between the parameter names here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure.

(DataFrame([[0.123456, 0.234567, 0.567567],
[12.32112, 123123.2, 321321.2]],
index=['A', 'B'], columns=['X', 'Y', 'Z']), None),
(DataFrame([['abc', 'def', 'ghi']], columns=['X', 'Y', 'Z']), 'ascii'),
(DataFrame(5 * [[123, u"你好", u"世界"]],
columns=['X', 'Y', 'Z']), 'gb2312'),
(DataFrame(5 * [[123, u"Γειά σου", u"Κόσμε"]],
columns=['X', 'Y', 'Z']), 'cp737')
])
def test_to_csv_compression(self, frame, encoding, compression):

Copy link
Contributor

Choose a reason for hiding this comment

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

same comment as below, add the issue numbers as comments

with ensure_clean() as filename:

df.to_csv(filename, compression=compression)
frame.to_csv(filename, compression=compression, encoding=encoding)

# test the round trip - to_csv -> read_csv
rs = read_csv(filename, compression=compression,
Copy link
Member

Choose a reason for hiding this comment

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

Can you change rs to result?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

index_col=0)
assert_frame_equal(df, rs)
index_col=0, encoding=encoding)
assert_frame_equal(frame, rs)

# explicitly make sure file is compressed
with tm.decompress_file(filename, compression) as fh:
text = fh.read().decode('utf8')
for col in df.columns:
text = fh.read().decode(encoding or 'utf8')
for col in frame.columns:
assert col in text

with tm.decompress_file(filename, compression) as fh:
assert_frame_equal(df, read_csv(fh, index_col=0))
assert_frame_equal(frame, read_csv(fh,
index_col=0,
encoding=encoding))

def test_to_csv_date_format(self):
with ensure_clean('__tmp_to_csv_date_format__') as path:
Expand Down
22 changes: 14 additions & 8 deletions pandas/tests/series/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,35 @@ def test_to_csv_path_is_none(self):
csv_str = s.to_csv(path=None)
assert isinstance(csv_str, str)

def test_to_csv_compression(self, compression):

s = Series([0.123456, 0.234567, 0.567567], index=['A', 'B', 'C'],
name='X')
@pytest.mark.parametrize('s, encoding', [
Copy link
Member

Choose a reason for hiding this comment

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

Remove whitespace between parameter names

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

(Series([0.123456, 0.234567, 0.567567], index=['A', 'B', 'C'],
name='X'), None),
(Series(['abc', 'def', 'ghi'], name='X'), 'ascii'),
(Series(["123", u"你好", u"世界"], name=u"中文"), 'gb2312'),
(Series(["123", u"Γειά σου", u"Κόσμε"], name=u"Ελληνικά"), 'cp737')
])
def test_to_csv_compression(self, s, encoding, compression):

Copy link
Contributor

Choose a reason for hiding this comment

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

can you add the releveant issues here as comments (e.g. the issues you are closing)

with ensure_clean() as filename:

s.to_csv(filename, compression=compression, header=True)
s.to_csv(filename, compression=compression, encoding=encoding,
header=True)

# test the round trip - to_csv -> read_csv
rs = pd.read_csv(filename, compression=compression,
index_col=0, squeeze=True)
encoding=encoding, index_col=0, squeeze=True)
assert_series_equal(s, rs)

# explicitly ensure file was compressed
with tm.decompress_file(filename, compression) as fh:
text = fh.read().decode('utf8')
text = fh.read().decode(encoding or 'utf8')
assert s.name in text

with tm.decompress_file(filename, compression) as fh:
assert_series_equal(s, pd.read_csv(fh,
index_col=0,
squeeze=True))
squeeze=True,
encoding=encoding))


class TestSeriesIO(TestData):
Expand Down