Skip to content

Commit 9313f37

Browse files
committed
move decompress_file to util/testing.py
1 parent 42ceff8 commit 9313f37

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

pandas/tests/series/test_io.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,6 @@ def test_to_csv_path_is_none(self):
139139
csv_str = s.to_csv(path=None)
140140
assert isinstance(csv_str, str)
141141

142-
def decompress_file(self, src_path, compression):
143-
if compression is None:
144-
f = open(src_path, 'rb')
145-
elif compression == 'gzip':
146-
import gzip
147-
f = gzip.open(src_path, 'rb')
148-
elif compression == 'bz2':
149-
import bz2
150-
f = bz2.BZ2File(src_path, 'rb')
151-
elif compression == 'xz':
152-
lzma = compat.import_lzma()
153-
f = lzma.LZMAFile(src_path, 'rb')
154-
else:
155-
msg = 'Unrecognized compression type: {}'.format(compression)
156-
raise ValueError(msg)
157-
158-
return f
159-
160142
@pytest.mark.parametrize('compression', [
161143
None,
162144
'gzip',
@@ -178,12 +160,12 @@ def test_to_csv_compression(self, compression):
178160
assert_series_equal(s, rs)
179161

180162
# explicitly ensure file was compressed
181-
f = self.decompress_file(filename, compression=compression)
163+
f = tm.decompress_file(filename, compression=compression)
182164
text = f.read().decode('utf8')
183165
assert s.name in text
184166
f.close()
185167

186-
f = self.decompress_file(filename, compression=compression)
168+
f = tm.decompress_file(filename, compression=compression)
187169
assert_series_equal(s, pd.read_csv(f, index_col=0, squeeze=True))
188170
f.close()
189171

pandas/util/testing.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,40 @@ def round_trip_localpath(writer, reader, path=None):
162162
return obj
163163

164164

165+
def decompress_file(path, compression):
166+
"""
167+
Open a compressed file and return a file object
168+
169+
Parameters
170+
----------
171+
path : str
172+
The path where the file is read from
173+
174+
compression : {'gzip', 'bz2', 'xz', None}
175+
Name of the decompression to use
176+
177+
Returns
178+
-------
179+
f : file object
180+
"""
181+
182+
if compression is None:
183+
f = open(path, 'rb')
184+
elif compression == 'gzip':
185+
import gzip
186+
f = gzip.open(path, 'rb')
187+
elif compression == 'bz2':
188+
import bz2
189+
f = bz2.BZ2File(path, 'rb')
190+
elif compression == 'xz':
191+
lzma = compat.import_lzma()
192+
f = lzma.LZMAFile(path, 'rb')
193+
else:
194+
msg = 'Unrecognized compression type: {}'.format(compression)
195+
raise ValueError(msg)
196+
197+
return f
198+
165199
def assert_almost_equal(left, right, check_exact=False,
166200
check_dtype='equiv', check_less_precise=False,
167201
**kwargs):

0 commit comments

Comments
 (0)