How to load python pickle from Julia?
Asked Answered
S

2

6

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

Sturdivant answered 14/1, 2021 at 14:11 Comment(3)
Given that there are over 200 existing questions on Stackoverflow about python pickling, I think this is on-topic.Sturdivant
I don't you understand what I've written. seeking recommendations for packages is not! stackoverflow.com/help/on-topicLinzy
and now you need to give a minimal reproducible example of what you have tried and the errors you're facingLinzy
S
10

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.

Sturdivant answered 14/1, 2021 at 14:11 Comment(0)
T
0

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
Torp answered 30/8, 2022 at 8:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.