Skip to content

Commit 0f925c1

Browse files
committed
Test _infer_compression in io/test_common.py
1 parent 8fcf398 commit 0f925c1

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

pandas/tests/io/parser/compression.py

-12
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,6 @@ def test_read_csv_infer_compression(self):
157157

158158
inputs[3].close()
159159

160-
@pytest.mark.parametrize('extension', ['', '.gz', '.bz2'])
161-
def test_read_csv_infer_compression_pathlib(self, extension):
162-
"""
163-
Test that compression is inferred from pathlib.Path paths.
164-
"""
165-
pathlib = pytest.importorskip('pathlib')
166-
read_csv_kwargs = {'index_col': 0, 'parse_dates': True}
167-
expected = self.read_csv(self.csv1, **read_csv_kwargs)
168-
path = pathlib.Path(self.csv1 + extension)
169-
df = self.read_csv(path, compression='infer', **read_csv_kwargs)
170-
tm.assert_frame_equal(expected, df)
171-
172160
def test_invalid_compression(self):
173161
msg = 'Unrecognized compression type: sfark'
174162
with tm.assert_raises_regex(ValueError, msg):

pandas/tests/io/test_common.py

+28-10
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@
1414

1515
from pandas import read_csv, concat
1616

17-
try:
18-
from pathlib import Path
19-
except ImportError:
20-
pass
21-
22-
try:
23-
from py.path import local as LocalPath
24-
except ImportError:
25-
pass
26-
2717

2818
class CustomFSPath(object):
2919
"""For testing fspath on unknown objects"""
@@ -34,6 +24,21 @@ def __fspath__(self):
3424
return self.path
3525

3626

27+
# Functions that consume a string path and return a string or path-like object
28+
path_types = [str, CustomFSPath]
29+
30+
try:
31+
from pathlib import Path
32+
path_types.append(Path)
33+
except ImportError:
34+
pass
35+
36+
try:
37+
from py.path import local as LocalPath
38+
path_types.append(LocalPath)
39+
except ImportError:
40+
pass
41+
3742
HERE = os.path.dirname(__file__)
3843

3944

@@ -83,6 +88,19 @@ def test_stringify_path_fspath(self):
8388
result = common._stringify_path(p)
8489
assert result == 'foo/bar.csv'
8590

91+
@pytest.mark.parametrize('extension,expected', [
92+
('', None),
93+
('.gz', 'gzip'),
94+
('.bz2', 'bz2'),
95+
('.zip', 'zip'),
96+
('.xz', 'xz'),
97+
])
98+
@pytest.mark.parametrize('path_type', path_types)
99+
def test_infer_compression_from_path(self, extension, expected, path_type):
100+
path = path_type('foo/bar.csv' + extension)
101+
compression = common._infer_compression(path, compression='infer')
102+
assert compression == expected
103+
86104
def test_get_filepath_or_buffer_with_path(self):
87105
filename = '~/sometest'
88106
filepath_or_buffer, _, _ = common.get_filepath_or_buffer(filename)

0 commit comments

Comments
 (0)