Skip to content

ENH Enable bzip2 streaming for Python 3 #11072

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 1 commit into from
Sep 13, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@ Other enhancements

- Improved error message when concatenating an empty iterable of dataframes (:issue:`9157`)

- ``pd.read_csv`` can now read bz2-compressed files incrementally, and the C parser can read bz2-compressed files from AWS S3 (:issue:`11070`, :issue:`11072`).


.. _whatsnew_0170.api:

Expand Down
11 changes: 6 additions & 5 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,12 +1344,13 @@ def _wrap_compressed(f, compression, encoding=None):
elif compression == 'bz2':
import bz2

# bz2 module can't take file objects, so have to run through decompress
# manually
data = bz2.decompress(f.read())
if compat.PY3:
data = data.decode(encoding)
f = StringIO(data)
f = bz2.open(f, 'rt', encoding=encoding)
else:
# Python 2's bz2 module can't take file objects, so have to
# run through decompress manually
data = bz2.decompress(f.read())
f = StringIO(data)
return f
else:
raise ValueError('do not recognize compression method %s'
Expand Down
8 changes: 8 additions & 0 deletions pandas/io/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3836,6 +3836,14 @@ def test_decompression(self):
self.assertRaises(ValueError, self.read_csv,
path, compression='bz3')

with open(path, 'rb') as fin:
if compat.PY3:
result = self.read_csv(fin, compression='bz2')
tm.assert_frame_equal(result, expected)
else:
self.assertRaises(ValueError, self.read_csv,
fin, compression='bz2')

def test_decompression_regex_sep(self):
try:
import gzip
Expand Down
4 changes: 2 additions & 2 deletions pandas/parser.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,10 @@ cdef class TextReader:
source = gzip.GzipFile(fileobj=source)
elif self.compression == 'bz2':
import bz2
if isinstance(source, basestring):
if isinstance(source, basestring) or PY3:
source = bz2.BZ2File(source, 'rb')
else:
raise ValueError('Python cannot read bz2 from open file '
raise ValueError('Python 2 cannot read bz2 from open file '
'handle')
else:
raise ValueError('Unrecognized compression type: %s' %
Expand Down