Skip to content

Commit 51d7bca

Browse files
Fix zlib and blosc imports
1 parent 10c933b commit 51d7bca

File tree

1 file changed

+5
-29
lines changed

1 file changed

+5
-29
lines changed

pandas/io/packers.py

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,7 @@
6565
# until we can pass this into our conversion functions,
6666
# this is pretty hacky
6767
compressor = None
68-
_IMPORTS = False
69-
_BLOSC = False
7068

71-
def _importers():
72-
# import things we need
73-
# but make this done on a first use basis
74-
75-
global _IMPORTS
76-
if _IMPORTS:
77-
return
78-
79-
_IMPORTS = True
80-
81-
global _BLOSC
82-
import zlib
83-
try:
84-
import blosc
85-
_BLOSC = True
86-
except:
87-
pass
8869

8970
def to_msgpack(path_or_buf, *args, **kwargs):
9071
"""
@@ -103,7 +84,6 @@ def to_msgpack(path_or_buf, *args, **kwargs):
10384
compress : type of compressor (zlib or blosc), default to None (no
10485
compression)
10586
"""
106-
_importers()
10787
global compressor
10888
compressor = kwargs.pop('compress', None)
10989
append = kwargs.pop('append', None)
@@ -146,7 +126,6 @@ def read_msgpack(path_or_buf, iterator=False, **kwargs):
146126
obj : type of object stored in file
147127
148128
"""
149-
_importers()
150129
path_or_buf, _ = get_filepath_or_buffer(path_or_buf)
151130
if iterator:
152131
return Iterator(path_or_buf)
@@ -232,16 +211,18 @@ def convert(values):
232211

233212
# convert to a bytes array
234213
v = v.tostring()
214+
import zlib
235215
return zlib.compress(v)
236216

237-
elif compressor == 'blosc' and _BLOSC:
217+
elif compressor == 'blosc':
238218

239219
# return string arrays like they are
240220
if dtype == np.object_:
241221
return v.tolist()
242222

243223
# convert to a bytes array
244224
v = v.tostring()
225+
import blosc
245226
return blosc.compress(v, typesize=dtype.itemsize)
246227

247228
# ndarray (on original dtype)
@@ -254,18 +235,13 @@ def unconvert(values, dtype, compress=None):
254235
return np.array(values, dtype=object)
255236

256237
if compress == 'zlib':
257-
238+
import zlib
258239
values = zlib.decompress(values)
259240
return np.frombuffer(values, dtype=dtype)
260241

261242
elif compress == 'blosc':
262-
263-
if not _BLOSC:
264-
raise Exception("cannot uncompress w/o blosc")
265-
266-
# decompress
243+
import blosc
267244
values = blosc.decompress(values)
268-
269245
return np.frombuffer(values, dtype=dtype)
270246

271247
# from a string

0 commit comments

Comments
 (0)