How to load python pickle from Julia?

Viewed 595

How can I load a python .pkl (pickle) file from Julia?

2 Answers

Here is a snippet to load in pickle files directly with PyCall:

using PyCall

py"""
import pickle
 
def load_pickle(fpath):
    with open(fpath, "rb") as f:
        data = pickle.load(f)
    return data
"""

load_pickle = py"load_pickle"

Then use load_pickle("<path to file>.pkl") and it should load it into a Julia Dict.

Another way is to use Pandas.jl to read the .pkl file:

julia> using Pandas

julia> df = read_pickle("<FILE_NAME>.pkl");

Also, You can convert it to a DataFrames.DataFrame object in this way:

julia> using DataFrames
julia> df1 = DataFrames.DataFrame(df);

Then if you check for the type of each object:

julia> typeof(df)
Pandas.DataFrame

julia> typeof(df1)
DataFrames.DataFrame
Related