-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
ENH: Add tranparent compression to json reading/writing #17798
Merged
TomAugspurger
merged 6 commits into
pandas-dev:master
from
simongibbons:add-json-compression
Oct 6, 2017
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9f2af42
ENH: Add tranparent compression to json reading/writing
simongibbons 3ed830c
Fix PEP8 violations
simongibbons 2a7c3b2
Add PR number to whatsnew entry
simongibbons 8e9fd4a
Remove problematic Windows test (The S3 test hits the same edge case)
simongibbons ff98b60
Extract decompress file function so that pytest.paramatrize can be us…
simongibbons 402fa11
Fix typo in whatsnew entry
simongibbons File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import pytest | ||
import moto | ||
|
||
import pandas as pd | ||
from pandas import compat | ||
import pandas.util.testing as tm | ||
from pandas.util.testing import assert_frame_equal, assert_raises_regex | ||
|
||
|
||
COMPRESSION_TYPES = [None, 'bz2', 'gzip', 'xz'] | ||
|
||
|
||
def decompress_file(path, compression): | ||
if compression is None: | ||
f = open(path, 'rb') | ||
elif compression == 'gzip': | ||
import gzip | ||
f = gzip.GzipFile(path, 'rb') | ||
elif compression == 'bz2': | ||
import bz2 | ||
f = bz2.BZ2File(path, 'rb') | ||
elif compression == 'xz': | ||
lzma = compat.import_lzma() | ||
f = lzma.open(path, 'rb') | ||
else: | ||
msg = 'Unrecognized compression type: {}'.format(compression) | ||
raise ValueError(msg) | ||
|
||
result = f.read().decode('utf8') | ||
f.close() | ||
return result | ||
|
||
|
||
@pytest.mark.parametrize('compression', COMPRESSION_TYPES) | ||
def test_compression_roundtrip(compression): | ||
if compression == 'xz': | ||
tm._skip_if_no_lzma() | ||
|
||
df = pd.DataFrame([[0.123456, 0.234567, 0.567567], | ||
[12.32112, 123123.2, 321321.2]], | ||
index=['A', 'B'], columns=['X', 'Y', 'Z']) | ||
|
||
with tm.ensure_clean() as path: | ||
df.to_json(path, compression=compression) | ||
assert_frame_equal(df, pd.read_json(path, compression=compression)) | ||
|
||
# explicitly ensure file was compressed. | ||
uncompressed_content = decompress_file(path, compression) | ||
assert_frame_equal(df, pd.read_json(uncompressed_content)) | ||
|
||
|
||
def test_compress_zip_value_error(): | ||
df = pd.DataFrame([[0.123456, 0.234567, 0.567567], | ||
[12.32112, 123123.2, 321321.2]], | ||
index=['A', 'B'], columns=['X', 'Y', 'Z']) | ||
|
||
with tm.ensure_clean() as path: | ||
import zipfile | ||
pytest.raises(zipfile.BadZipfile, df.to_json, path, compression="zip") | ||
|
||
|
||
def test_read_zipped_json(): | ||
uncompressed_path = tm.get_data_path("tsframe_v012.json") | ||
uncompressed_df = pd.read_json(uncompressed_path) | ||
|
||
compressed_path = tm.get_data_path("tsframe_v012.json.zip") | ||
compressed_df = pd.read_json(compressed_path, compression='zip') | ||
|
||
assert_frame_equal(uncompressed_df, compressed_df) | ||
|
||
|
||
@pytest.mark.parametrize('compression', COMPRESSION_TYPES) | ||
def test_with_s3_url(compression): | ||
boto3 = pytest.importorskip('boto3') | ||
pytest.importorskip('s3fs') | ||
if compression == 'xz': | ||
tm._skip_if_no_lzma() | ||
|
||
df = pd.read_json('{"a": [1, 2, 3], "b": [4, 5, 6]}') | ||
with moto.mock_s3(): | ||
conn = boto3.resource("s3", region_name="us-east-1") | ||
bucket = conn.create_bucket(Bucket="pandas-test") | ||
|
||
with tm.ensure_clean() as path: | ||
df.to_json(path, compression=compression) | ||
with open(path, 'rb') as f: | ||
bucket.put_object(Key='test-1', Body=f) | ||
|
||
roundtripped_df = pd.read_json('s3://pandas-test/test-1', | ||
compression=compression) | ||
assert_frame_equal(df, roundtripped_df) | ||
|
||
|
||
@pytest.mark.parametrize('compression', COMPRESSION_TYPES) | ||
def test_lines_with_compression(compression): | ||
if compression == 'xz': | ||
tm._skip_if_no_lzma() | ||
|
||
with tm.ensure_clean() as path: | ||
df = pd.read_json('{"a": [1, 2, 3], "b": [4, 5, 6]}') | ||
df.to_json(path, orient='records', lines=True, compression=compression) | ||
roundtripped_df = pd.read_json(path, lines=True, | ||
compression=compression) | ||
assert_frame_equal(df, roundtripped_df) | ||
|
||
|
||
@pytest.mark.parametrize('compression', COMPRESSION_TYPES) | ||
def test_chunksize_with_compression(compression): | ||
if compression == 'xz': | ||
tm._skip_if_no_lzma() | ||
|
||
with tm.ensure_clean() as path: | ||
df = pd.read_json('{"a": ["foo", "bar", "baz"], "b": [4, 5, 6]}') | ||
df.to_json(path, orient='records', lines=True, compression=compression) | ||
|
||
roundtripped_df = pd.concat(pd.read_json(path, lines=True, chunksize=1, | ||
compression=compression)) | ||
assert_frame_equal(df, roundtripped_df) | ||
|
||
|
||
def test_write_unsupported_compression_type(): | ||
df = pd.read_json('{"a": [1, 2, 3], "b": [4, 5, 6]}') | ||
with tm.ensure_clean() as path: | ||
msg = "Unrecognized compression type: unsupported" | ||
assert_raises_regex(ValueError, msg, df.to_json, | ||
path, compression="unsupported") | ||
|
||
|
||
def test_read_unsupported_compression_type(): | ||
with tm.ensure_clean() as path: | ||
msg = "Unrecognized compression type: unsupported" | ||
assert_raises_regex(ValueError, msg, pd.read_json, | ||
path, compression="unsupported") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shares some code with the (to be merged) #17201
I think it's fine for now, but we'll want to clean it up whenever the later is merged. Since this is clean at the moment, I think we'll merge it, and then refactor this test in #17201.