1
1
import pytest
2
2
3
3
import pandas as pd
4
- from pandas import compat
5
4
import pandas .util .testing as tm
6
- import pandas .util ._test_decorators as td
7
5
from pandas .util .testing import assert_frame_equal , assert_raises_regex
8
6
9
7
10
- COMPRESSION_TYPES = [None , 'bz2' , 'gzip' ,
11
- pytest .param ('xz' , marks = td .skip_if_no_lzma )]
12
-
13
-
14
- def decompress_file (path , compression ):
15
- if compression is None :
16
- f = open (path , 'rb' )
17
- elif compression == 'gzip' :
18
- import gzip
19
- f = gzip .GzipFile (path , 'rb' )
20
- elif compression == 'bz2' :
21
- import bz2
22
- f = bz2 .BZ2File (path , 'rb' )
23
- elif compression == 'xz' :
24
- lzma = compat .import_lzma ()
25
- f = lzma .open (path , 'rb' )
26
- else :
27
- msg = 'Unrecognized compression type: {}' .format (compression )
28
- raise ValueError (msg )
29
-
30
- result = f .read ().decode ('utf8' )
31
- f .close ()
32
- return result
33
-
34
-
35
- @pytest .mark .parametrize ('compression' , COMPRESSION_TYPES )
36
8
def test_compression_roundtrip (compression ):
37
9
df = pd .DataFrame ([[0.123456 , 0.234567 , 0.567567 ],
38
10
[12.32112 , 123123.2 , 321321.2 ]],
@@ -43,8 +15,9 @@ def test_compression_roundtrip(compression):
43
15
assert_frame_equal (df , pd .read_json (path , compression = compression ))
44
16
45
17
# explicitly ensure file was compressed.
46
- uncompressed_content = decompress_file (path , compression )
47
- assert_frame_equal (df , pd .read_json (uncompressed_content ))
18
+ with tm .decompress_file (path , compression ) as fh :
19
+ result = fh .read ().decode ('utf8' )
20
+ assert_frame_equal (df , pd .read_json (result ))
48
21
49
22
50
23
def test_compress_zip_value_error ():
@@ -67,7 +40,6 @@ def test_read_zipped_json():
67
40
assert_frame_equal (uncompressed_df , compressed_df )
68
41
69
42
70
- @pytest .mark .parametrize ('compression' , COMPRESSION_TYPES )
71
43
def test_with_s3_url (compression ):
72
44
boto3 = pytest .importorskip ('boto3' )
73
45
pytest .importorskip ('s3fs' )
@@ -88,7 +60,6 @@ def test_with_s3_url(compression):
88
60
assert_frame_equal (df , roundtripped_df )
89
61
90
62
91
- @pytest .mark .parametrize ('compression' , COMPRESSION_TYPES )
92
63
def test_lines_with_compression (compression ):
93
64
with tm .ensure_clean () as path :
94
65
df = pd .read_json ('{"a": [1, 2, 3], "b": [4, 5, 6]}' )
@@ -98,7 +69,6 @@ def test_lines_with_compression(compression):
98
69
assert_frame_equal (df , roundtripped_df )
99
70
100
71
101
- @pytest .mark .parametrize ('compression' , COMPRESSION_TYPES )
102
72
def test_chunksize_with_compression (compression ):
103
73
with tm .ensure_clean () as path :
104
74
df = pd .read_json ('{"a": ["foo", "bar", "baz"], "b": [4, 5, 6]}' )
0 commit comments