Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading pickle in R

Tags:

python

r

pickle

I am trying to load a pickle object in R, using the following process found online.

First, I create a Python file called: "pickle_reader.py":

import pandas as pd
def read_pickle_file(file):
    pickle_data = pd.read_pickle(file)
    return pickle_data

Then, I run the following R code:

install.packages('reticulate')

require("reticulate")

source_python("pickle_reader.py")
pickle_data <- read_pickle_file("pathname") 

but I get an error that says:

Error in py_run_file_impl(file, local, convert) : ImportError: No module named pandas

N.B. I tried installing pandas again but this doesn't change the issue.

Do you know how should I proceed?

like image 260
Eaglez Avatar asked Sep 05 '25 03:09

Eaglez


1 Answers

I find this to be a much more straight forward method than making a new .py file. In an R code chunk do:

library(reticulate)

pd <- import("pandas")

x <- pd$read_pickle("file.pickle")
like image 90
Stephen Williams Avatar answered Sep 08 '25 00:09

Stephen Williams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!