def read_pickle(path): """ Load pickled pandas object (or any other pickled object) from the specified file path Warning: Loading pickled data received from untrusted sources can be unsafe. See: http://docs.python.org/2.7/library/pickle.html Parameters ---------- path : string File path Returns ------- unpickled : type of object stored in file """ try: with open(path, 'rb') as fh: return pkl.load(fh) except: if PY3: with open(path, 'rb') as fh: return pkl.load(fh, encoding='latin1') raise
def setUpClass(cls): if compat.PY3: raise nose.SkipTest("not compatible with Python >= 3") pth, _ = os.path.split(os.path.abspath(__file__)) filepath = os.path.join(pth, 'data', 'frame.pickle') with open(filepath, 'rb') as f: cls.frame = pickle.load(f) filepath = os.path.join(pth, 'data', 'series.pickle') with open(filepath, 'rb') as f: cls.series = pickle.load(f)
def try_read(path, encoding=None): # try with cPickle # try with current pickle, if we have a Type Error then # try with the compat pickle to handle subclass changes # pass encoding only if its not None as py2 doesn't handle # the param # cpickle # GH 6899 try: with warnings.catch_warnings(record=True): # We want to silence any warnings about, e.g. moved modules. warnings.simplefilter("ignore", Warning) return read_wrapper(lambda f: pkl.load(f)) except Exception: # noqa: E722 # reg/patched pickle # compat not used in pandas/compat/pickle_compat.py::load # TODO: remove except block OR modify pc.load to use compat try: return read_wrapper( lambda f: pc.load(f, encoding=encoding, compat=False)) # compat pickle except Exception: # noqa: E722 return read_wrapper( lambda f: pc.load(f, encoding=encoding, compat=True))
def try_read(path, encoding=None): # try with cPickle # try with current pickle, if we have a Type Error then # try with the compat pickle to handle subclass changes # pass encoding only if its not None as py2 doesn't handle # the param # cpickle # GH 6899 try: return read_wrapper(lambda f: pkl.load(f)) except Exception: # reg/patched pickle try: return read_wrapper( lambda f: pc.load(f, encoding=encoding, compat=False)) # compat pickle except: return read_wrapper( lambda f: pc.load(f, encoding=encoding, compat=True))
def try_read(path, encoding=None): # try with cPickle # try with current pickle, if we have a Type Error then # try with the compat pickle to handle subclass changes # pass encoding only if its not None as py2 doesn't handle # the param # cpickle # GH 6899 try: with warnings.catch_warnings(record=True): # We want to silencce any warnings about, e.g. moved modules. return read_wrapper(lambda f: pkl.load(f)) except Exception: # reg/patched pickle try: return read_wrapper( lambda f: pc.load(f, encoding=encoding, compat=False)) # compat pickle except: return read_wrapper( lambda f: pc.load(f, encoding=encoding, compat=True))
def try_read(path, encoding=None): # try with cPickle # try with current pickle, if we have a Type Error then # try with the compat pickle to handle subclass changes # pass encoding only if its not None as py2 doesn't handle # the param # cpickle # GH 6899 try: with open(path, 'rb') as fh: return pkl.load(fh) except Exception: # reg/patched pickle try: with open(path, 'rb') as fh: return pc.load(fh, encoding=encoding, compat=False) # compat pickle except: with open(path, 'rb') as fh: return pc.load(fh, encoding=encoding, compat=True)
def read_pickle(path, compression='infer'): """ Load pickled pandas object (or any object) from file. .. warning:: Loading pickled data received from untrusted sources can be unsafe. See `here <https://docs.python.org/3/library/pickle.html>`__. Parameters ---------- path : str File path where the pickled object will be loaded. compression : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}, default 'infer' For on-the-fly decompression of on-disk data. If 'infer', then use gzip, bz2, xz or zip if path ends in '.gz', '.bz2', '.xz', or '.zip' respectively, and no decompression otherwise. Set to None for no decompression. .. versionadded:: 0.20.0 Returns ------- unpickled : same type as object stored in file See Also -------- DataFrame.to_pickle : Pickle (serialize) DataFrame object to file. Series.to_pickle : Pickle (serialize) Series object to file. read_hdf : Read HDF5 file into a DataFrame. read_sql : Read SQL query or database table into a DataFrame. read_parquet : Load a parquet object, returning a DataFrame. Examples -------- >>> original_df = pd.DataFrame({"foo": range(5), "bar": range(5, 10)}) >>> original_df foo bar 0 0 5 1 1 6 2 2 7 3 3 8 4 4 9 >>> pd.to_pickle(original_df, "./dummy.pkl") >>> unpickled_df = pd.read_pickle("./dummy.pkl") >>> unpickled_df foo bar 0 0 5 1 1 6 2 2 7 3 3 8 4 4 9 >>> import os >>> os.remove("./dummy.pkl") """ path = _stringify_path(path) f, fh = _get_handle(path, 'rb', compression=compression, is_text=False) # 1) try with cPickle # 2) try with the compat pickle to handle subclass changes # 3) pass encoding only if its not None as py2 doesn't handle the param try: with warnings.catch_warnings(record=True): # We want to silence any warnings about, e.g. moved modules. warnings.simplefilter("ignore", Warning) return pkl.load(f) except Exception: # noqa: E722 try: return pc.load(f, encoding=None) except Exception: # noqa: E722 if PY3: return pc.load(f, encoding='latin1') raise finally: f.close() for _f in fh: _f.close()