Skip to content

Commit 84881db

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

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,39 @@ def round_trip_localpath(writer, reader, path=None):
161161
obj = reader(LocalPath(path))
162162
return obj
163163

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

165198
def assert_almost_equal(left, right, check_exact=False,
166199
check_dtype='equiv', check_less_precise=False,

0 commit comments

Comments
 (0)