Skip to content

Commit 16328e0

Browse files
committed
create compression_no_zip fixture
1 parent 7af6e74 commit 16328e0

File tree

5 files changed

+81
-75
lines changed

5 files changed

+81
-75
lines changed

pandas/conftest.py

+10
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,13 @@ def compression(request):
8383
Fixture for trying common compression types in compression tests
8484
"""
8585
return request.param
86+
87+
88+
@pytest.fixture(params=[None, 'gzip', 'bz2',
89+
pytest.param('xz', marks=td.skip_if_no_lzma)])
90+
def compression_no_zip(request):
91+
"""
92+
Fixture for trying common compression types in compression tests
93+
except zip
94+
"""
95+
return request.param

pandas/tests/frame/test_to_csv.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -919,29 +919,29 @@ def test_to_csv_path_is_none(self):
919919
recons = pd.read_csv(StringIO(csv_str), index_col=0)
920920
assert_frame_equal(self.frame, recons)
921921

922-
def test_to_csv_compression(self, compression):
922+
def test_to_csv_compression(self, compression_no_zip):
923923

924924
df = DataFrame([[0.123456, 0.234567, 0.567567],
925925
[12.32112, 123123.2, 321321.2]],
926926
index=['A', 'B'], columns=['X', 'Y', 'Z'])
927927

928-
if compression != "zip":
929-
with ensure_clean() as filename:
928+
with ensure_clean() as filename:
930929

931-
df.to_csv(filename, compression=compression)
930+
df.to_csv(filename, compression=compression_no_zip)
932931

933-
# test the round trip - to_csv -> read_csv
934-
rs = read_csv(filename, compression=compression, index_col=0)
935-
assert_frame_equal(df, rs)
932+
# test the round trip - to_csv -> read_csv
933+
rs = read_csv(filename, compression=compression_no_zip,
934+
index_col=0)
935+
assert_frame_equal(df, rs)
936936

937-
# explicitly make sure file is compressed
938-
with tm.decompress_file(filename, compression) as fh:
939-
text = fh.read().decode('utf8')
940-
for col in df.columns:
941-
assert col in text
937+
# explicitly make sure file is compressed
938+
with tm.decompress_file(filename, compression_no_zip) as fh:
939+
text = fh.read().decode('utf8')
940+
for col in df.columns:
941+
assert col in text
942942

943-
with tm.decompress_file(filename, compression) as fh:
944-
assert_frame_equal(df, read_csv(fh, index_col=0))
943+
with tm.decompress_file(filename, compression_no_zip) as fh:
944+
assert_frame_equal(df, read_csv(fh, index_col=0))
945945

946946
def test_to_csv_compression_value_error(self):
947947
# GH7615

pandas/tests/io/json/test_compression.py

+30-32
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
from pandas.util.testing import assert_frame_equal, assert_raises_regex
66

77

8-
def test_compression_roundtrip(compression):
8+
def test_compression_roundtrip(compression_no_zip):
99
df = pd.DataFrame([[0.123456, 0.234567, 0.567567],
1010
[12.32112, 123123.2, 321321.2]],
1111
index=['A', 'B'], columns=['X', 'Y', 'Z'])
1212

13-
if compression != 'zip':
14-
with tm.ensure_clean() as path:
15-
df.to_json(path, compression=compression)
16-
assert_frame_equal(df, pd.read_json(path, compression=compression))
13+
with tm.ensure_clean() as path:
14+
df.to_json(path, compression=compression_no_zip)
15+
assert_frame_equal(df, pd.read_json(path,
16+
compression=compression_no_zip))
1717

18-
# explicitly ensure file was compressed.
19-
with tm.decompress_file(path, compression) as fh:
20-
result = fh.read().decode('utf8')
21-
assert_frame_equal(df, pd.read_json(result))
18+
# explicitly ensure file was compressed.
19+
with tm.decompress_file(path, compression_no_zip) as fh:
20+
result = fh.read().decode('utf8')
21+
assert_frame_equal(df, pd.read_json(result))
2222

2323

2424
def test_compress_zip_value_error():
@@ -41,7 +41,7 @@ def test_read_zipped_json():
4141
assert_frame_equal(uncompressed_df, compressed_df)
4242

4343

44-
def test_with_s3_url(compression):
44+
def test_with_s3_url(compression_no_zip):
4545
boto3 = pytest.importorskip('boto3')
4646
pytest.importorskip('s3fs')
4747
moto = pytest.importorskip('moto')
@@ -52,39 +52,37 @@ def test_with_s3_url(compression):
5252
bucket = conn.create_bucket(Bucket="pandas-test")
5353

5454
with tm.ensure_clean() as path:
55-
df.to_json(path, compression=compression)
55+
df.to_json(path, compression=compression_no_zip)
5656
with open(path, 'rb') as f:
5757
bucket.put_object(Key='test-1', Body=f)
5858

5959
roundtripped_df = pd.read_json('s3://pandas-test/test-1',
60-
compression=compression)
60+
compression=compression_no_zip)
6161
assert_frame_equal(df, roundtripped_df)
6262

6363

64-
def test_lines_with_compression(compression):
64+
def test_lines_with_compression(compression_no_zip):
6565

66-
if compression != 'zip':
67-
with tm.ensure_clean() as path:
68-
df = pd.read_json('{"a": [1, 2, 3], "b": [4, 5, 6]}')
69-
df.to_json(path, orient='records', lines=True,
70-
compression=compression)
71-
roundtripped_df = pd.read_json(path, lines=True,
72-
compression=compression)
73-
assert_frame_equal(df, roundtripped_df)
66+
with tm.ensure_clean() as path:
67+
df = pd.read_json('{"a": [1, 2, 3], "b": [4, 5, 6]}')
68+
df.to_json(path, orient='records', lines=True,
69+
compression=compression_no_zip)
70+
roundtripped_df = pd.read_json(path, lines=True,
71+
compression=compression_no_zip)
72+
assert_frame_equal(df, roundtripped_df)
7473

7574

76-
def test_chunksize_with_compression(compression):
75+
def test_chunksize_with_compression(compression_no_zip):
7776

78-
if compression != 'zip':
79-
with tm.ensure_clean() as path:
80-
df = pd.read_json('{"a": ["foo", "bar", "baz"], "b": [4, 5, 6]}')
81-
df.to_json(path, orient='records', lines=True,
82-
compression=compression)
83-
84-
roundtripped_df = pd.concat(pd.read_json(path, lines=True,
85-
chunksize=1,
86-
compression=compression))
87-
assert_frame_equal(df, roundtripped_df)
77+
with tm.ensure_clean() as path:
78+
df = pd.read_json('{"a": ["foo", "bar", "baz"], "b": [4, 5, 6]}')
79+
df.to_json(path, orient='records', lines=True,
80+
compression=compression_no_zip)
81+
82+
res = pd.read_json(path, lines=True, chunksize=1,
83+
compression=compression_no_zip)
84+
roundtripped_df = pd.concat(res)
85+
assert_frame_equal(df, roundtripped_df)
8886

8987

9088
def test_write_unsupported_compression_type():

pandas/tests/io/test_pickle.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -352,27 +352,26 @@ def compress_file(self, src_path, dest_path, compression):
352352
f.write(fh.read())
353353
f.close()
354354

355-
def test_write_explicit(self, compression, get_random_path):
355+
def test_write_explicit(self, compression_no_zip, get_random_path):
356356
base = get_random_path
357357
path1 = base + ".compressed"
358358
path2 = base + ".raw"
359359

360-
if compression != "zip":
361-
with tm.ensure_clean(path1) as p1, tm.ensure_clean(path2) as p2:
362-
df = tm.makeDataFrame()
360+
with tm.ensure_clean(path1) as p1, tm.ensure_clean(path2) as p2:
361+
df = tm.makeDataFrame()
363362

364-
# write to compressed file
365-
df.to_pickle(p1, compression=compression)
363+
# write to compressed file
364+
df.to_pickle(p1, compression=compression_no_zip)
366365

367-
# decompress
368-
with tm.decompress_file(p1, compression=compression) as f:
369-
with open(p2, "wb") as fh:
370-
fh.write(f.read())
366+
# decompress
367+
with tm.decompress_file(p1, compression=compression_no_zip) as f:
368+
with open(p2, "wb") as fh:
369+
fh.write(f.read())
371370

372-
# read decompressed file
373-
df2 = pd.read_pickle(p2, compression=None)
371+
# read decompressed file
372+
df2 = pd.read_pickle(p2, compression=None)
374373

375-
tm.assert_frame_equal(df, df2)
374+
tm.assert_frame_equal(df, df2)
376375

377376
@pytest.mark.parametrize('compression', ['', 'None', 'bad', '7z'])
378377
def test_write_explicit_bad(self, compression, get_random_path):

pandas/tests/series/test_io.py

+15-16
Original file line numberDiff line numberDiff line change
@@ -138,30 +138,29 @@ def test_to_csv_path_is_none(self):
138138
csv_str = s.to_csv(path=None)
139139
assert isinstance(csv_str, str)
140140

141-
def test_to_csv_compression(self, compression):
141+
def test_to_csv_compression(self, compression_no_zip):
142142

143143
s = Series([0.123456, 0.234567, 0.567567], index=['A', 'B', 'C'],
144144
name='X')
145145

146-
if compression != 'zip':
147-
with ensure_clean() as filename:
146+
with ensure_clean() as filename:
148147

149-
s.to_csv(filename, compression=compression, header=True)
148+
s.to_csv(filename, compression=compression_no_zip, header=True)
150149

151-
# test the round trip - to_csv -> read_csv
152-
rs = pd.read_csv(filename, compression=compression,
153-
index_col=0, squeeze=True)
154-
assert_series_equal(s, rs)
150+
# test the round trip - to_csv -> read_csv
151+
rs = pd.read_csv(filename, compression=compression_no_zip,
152+
index_col=0, squeeze=True)
153+
assert_series_equal(s, rs)
155154

156-
# explicitly ensure file was compressed
157-
with tm.decompress_file(filename, compression) as fh:
158-
text = fh.read().decode('utf8')
159-
assert s.name in text
155+
# explicitly ensure file was compressed
156+
with tm.decompress_file(filename, compression_no_zip) as fh:
157+
text = fh.read().decode('utf8')
158+
assert s.name in text
160159

161-
with tm.decompress_file(filename, compression) as fh:
162-
assert_series_equal(s, pd.read_csv(fh,
163-
index_col=0,
164-
squeeze=True))
160+
with tm.decompress_file(filename, compression_no_zip) as fh:
161+
assert_series_equal(s, pd.read_csv(fh,
162+
index_col=0,
163+
squeeze=True))
165164

166165

167166
class TestSeriesIO(TestData):

0 commit comments

Comments
 (0)