Skip to content

Commit 918c0f8

Browse files
committed
Move compression tests to new file tests/io/test_compression.py
1 parent f8829a6 commit 918c0f8

File tree

2 files changed

+103
-97
lines changed

2 files changed

+103
-97
lines changed

pandas/tests/io/test_common.py

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""
2-
Tests for the pandas.io.common functionalities
2+
Tests for the pandas.io.common functionalities
33
"""
44
import mmap
55
import os
6+
67
import pytest
78

89
import pandas as pd
@@ -286,99 +287,3 @@ def test_unknown_engine(self):
286287
df.to_csv(path)
287288
with tm.assert_raises_regex(ValueError, 'Unknown engine'):
288289
pd.read_csv(path, engine='pyt')
289-
290-
291-
@pytest.mark.parametrize('obj', [
292-
pd.DataFrame(100 * [[0.123456, 0.234567, 0.567567],
293-
[12.32112, 123123.2, 321321.2]],
294-
columns=['X', 'Y', 'Z']),
295-
pd.Series(100 * [0.123456, 0.234567, 0.567567], name='X')])
296-
@pytest.mark.parametrize('method', ['to_pickle', 'to_json', 'to_csv'])
297-
def test_compression_size(obj, method, compression_only):
298-
299-
with tm.ensure_clean() as path:
300-
getattr(obj, method)(path, compression=compression_only)
301-
compressed = os.path.getsize(path)
302-
getattr(obj, method)(path, compression=None)
303-
uncompressed = os.path.getsize(path)
304-
assert uncompressed > compressed
305-
306-
307-
@pytest.mark.parametrize('obj', [
308-
pd.DataFrame(100 * [[0.123456, 0.234567, 0.567567],
309-
[12.32112, 123123.2, 321321.2]],
310-
columns=['X', 'Y', 'Z']),
311-
pd.Series(100 * [0.123456, 0.234567, 0.567567], name='X')])
312-
@pytest.mark.parametrize('method', ['to_csv', 'to_json'])
313-
def test_compression_size_fh(obj, method, compression_only):
314-
315-
with tm.ensure_clean() as path:
316-
f, handles = icom._get_handle(path, 'w', compression=compression_only)
317-
with f:
318-
getattr(obj, method)(f)
319-
assert not f.closed
320-
assert f.closed
321-
compressed = os.path.getsize(path)
322-
with tm.ensure_clean() as path:
323-
f, handles = icom._get_handle(path, 'w', compression=None)
324-
with f:
325-
getattr(obj, method)(f)
326-
assert not f.closed
327-
assert f.closed
328-
uncompressed = os.path.getsize(path)
329-
assert uncompressed > compressed
330-
331-
332-
@pytest.mark.parametrize('write_method, write_kwargs, read_method', [
333-
('to_csv', {'index': False}, pd.read_csv),
334-
('to_json', {}, pd.read_json),
335-
('to_pickle', {}, pd.read_pickle),
336-
])
337-
def test_dataframe_compression_defaults_to_infer(
338-
write_method, write_kwargs, read_method, compression_only):
339-
# Test that DataFrame.to_* methods default to inferring compression from
340-
# paths. GH 22004
341-
input = pd.DataFrame([[1.0, 0, -4], [3.4, 5, 2]], columns=['X', 'Y', 'Z'])
342-
extension = icom._compression_to_extension[compression_only]
343-
with tm.ensure_clean('compressed' + extension) as path:
344-
getattr(input, write_method)(path, **write_kwargs)
345-
output = read_method(path, compression=compression_only)
346-
tm.assert_frame_equal(output, input)
347-
348-
349-
@pytest.mark.parametrize('write_method,write_kwargs,read_method,read_kwargs', [
350-
('to_csv', {'index': False, 'header': True},
351-
pd.read_csv, {'squeeze': True}),
352-
('to_json', {}, pd.read_json, {'typ': 'series'}),
353-
('to_pickle', {}, pd.read_pickle, {}),
354-
])
355-
def test_series_compression_defaults_to_infer(
356-
write_method, write_kwargs, read_method, read_kwargs,
357-
compression_only):
358-
# Test that Series.to_* methods default to inferring compression from
359-
# paths. GH 22004
360-
input = pd.Series([0, 5, -2, 10], name='X')
361-
extension = icom._compression_to_extension[compression_only]
362-
with tm.ensure_clean('compressed' + extension) as path:
363-
getattr(input, write_method)(path, **write_kwargs)
364-
output = read_method(path, compression=compression_only, **read_kwargs)
365-
tm.assert_series_equal(output, input, check_names=False)
366-
367-
368-
def test_compression_warning(compression_only):
369-
# Assert that passing a file object to to_csv while explicitly specifying a
370-
# compression protocol triggers a RuntimeWarning, as per GH 21227.
371-
# Note that pytest has an issue that causes assert_produces_warning to fail
372-
# in Python 2 if the warning has occurred in previous tests
373-
# (see https://git.io/fNEBm & https://git.io/fNEBC). Hence, should this
374-
# test fail in just Python 2 builds, it likely indicates that other tests
375-
# are producing RuntimeWarnings, thereby triggering the pytest bug.
376-
df = pd.DataFrame(100 * [[0.123456, 0.234567, 0.567567],
377-
[12.32112, 123123.2, 321321.2]],
378-
columns=['X', 'Y', 'Z'])
379-
with tm.ensure_clean() as path:
380-
f, handles = icom._get_handle(path, 'w', compression=compression_only)
381-
with tm.assert_produces_warning(RuntimeWarning,
382-
check_stacklevel=False):
383-
with f:
384-
df.to_csv(f, compression=compression_only)

pandas/tests/io/test_compression.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import os
2+
3+
import pytest
4+
5+
import pandas as pd
6+
import pandas.io.common as icom
7+
import pandas.util.testing as tm
8+
9+
10+
@pytest.mark.parametrize('obj', [
11+
pd.DataFrame(100 * [[0.123456, 0.234567, 0.567567],
12+
[12.32112, 123123.2, 321321.2]],
13+
columns=['X', 'Y', 'Z']),
14+
pd.Series(100 * [0.123456, 0.234567, 0.567567], name='X')])
15+
@pytest.mark.parametrize('method', ['to_pickle', 'to_json', 'to_csv'])
16+
def test_compression_size(obj, method, compression_only):
17+
with tm.ensure_clean() as path:
18+
getattr(obj, method)(path, compression=compression_only)
19+
compressed_size = os.path.getsize(path)
20+
getattr(obj, method)(path, compression=None)
21+
uncompressed_size = os.path.getsize(path)
22+
assert uncompressed_size > compressed_size
23+
24+
25+
@pytest.mark.parametrize('obj', [
26+
pd.DataFrame(100 * [[0.123456, 0.234567, 0.567567],
27+
[12.32112, 123123.2, 321321.2]],
28+
columns=['X', 'Y', 'Z']),
29+
pd.Series(100 * [0.123456, 0.234567, 0.567567], name='X')])
30+
@pytest.mark.parametrize('method', ['to_csv', 'to_json'])
31+
def test_compression_size_fh(obj, method, compression_only):
32+
with tm.ensure_clean() as path:
33+
f, handles = icom._get_handle(path, 'w', compression=compression_only)
34+
with f:
35+
getattr(obj, method)(f)
36+
assert not f.closed
37+
assert f.closed
38+
compressed_size = os.path.getsize(path)
39+
with tm.ensure_clean() as path:
40+
f, handles = icom._get_handle(path, 'w', compression=None)
41+
with f:
42+
getattr(obj, method)(f)
43+
assert not f.closed
44+
assert f.closed
45+
uncompressed_size = os.path.getsize(path)
46+
assert uncompressed_size > compressed_size
47+
48+
49+
@pytest.mark.parametrize('write_method, write_kwargs, read_method', [
50+
('to_csv', {'index': False}, pd.read_csv),
51+
('to_json', {}, pd.read_json),
52+
('to_pickle', {}, pd.read_pickle),
53+
])
54+
def test_dataframe_compression_defaults_to_infer(
55+
write_method, write_kwargs, read_method, compression_only):
56+
# Test that DataFrame.to_* methods default to inferring compression from
57+
# paths. GH 22004
58+
input = pd.DataFrame([[1.0, 0, -4], [3.4, 5, 2]], columns=['X', 'Y', 'Z'])
59+
extension = icom._compression_to_extension[compression_only]
60+
with tm.ensure_clean('compressed' + extension) as path:
61+
getattr(input, write_method)(path, **write_kwargs)
62+
output = read_method(path, compression=compression_only)
63+
tm.assert_frame_equal(output, input)
64+
65+
66+
@pytest.mark.parametrize('write_method,write_kwargs,read_method,read_kwargs', [
67+
('to_csv', {'index': False, 'header': True},
68+
pd.read_csv, {'squeeze': True}),
69+
('to_json', {}, pd.read_json, {'typ': 'series'}),
70+
('to_pickle', {}, pd.read_pickle, {}),
71+
])
72+
def test_series_compression_defaults_to_infer(
73+
write_method, write_kwargs, read_method, read_kwargs,
74+
compression_only):
75+
# Test that Series.to_* methods default to inferring compression from
76+
# paths. GH 22004
77+
input = pd.Series([0, 5, -2, 10], name='X')
78+
extension = icom._compression_to_extension[compression_only]
79+
with tm.ensure_clean('compressed' + extension) as path:
80+
getattr(input, write_method)(path, **write_kwargs)
81+
output = read_method(path, compression=compression_only, **read_kwargs)
82+
tm.assert_series_equal(output, input, check_names=False)
83+
84+
85+
def test_compression_warning(compression_only):
86+
# Assert that passing a file object to to_csv while explicitly specifying a
87+
# compression protocol triggers a RuntimeWarning, as per GH 21227.
88+
# Note that pytest has an issue that causes assert_produces_warning to fail
89+
# in Python 2 if the warning has occurred in previous tests
90+
# (see https://git.io/fNEBm & https://git.io/fNEBC). Hence, should this
91+
# test fail in just Python 2 builds, it likely indicates that other tests
92+
# are producing RuntimeWarnings, thereby triggering the pytest bug.
93+
df = pd.DataFrame(100 * [[0.123456, 0.234567, 0.567567],
94+
[12.32112, 123123.2, 321321.2]],
95+
columns=['X', 'Y', 'Z'])
96+
with tm.ensure_clean() as path:
97+
f, handles = icom._get_handle(path, 'w', compression=compression_only)
98+
with tm.assert_produces_warning(RuntimeWarning,
99+
check_stacklevel=False):
100+
with f:
101+
df.to_csv(f, compression=compression_only)

0 commit comments

Comments
 (0)