2

I'm using the R package PythonInR, and wanted to convert the pandas dataframe to R dataframe using function pyGet(), but I got the error below:

Error in as.data.frame.default(xi, optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""PythonObject"" to a data.frame

The text.csv file only contains two columns, name(string) and value(int). The code is as below:

library(PythonInR) 
pyConnect() 
pyIsConnected() 
pyVersion()
pyOptions("usePandas", TRUE) 
pyImport("pandas", as="pd") 
test_code <-'py_df = pd.read_csv("test.csv")' 
pyExec(code = test_code) 
r_df <- pyGet("py_df")

And the output of the code is:

library(PythonInR)
pyConnect()
R is already connected to Python!
pyIsConnected()
[1] TRUE
pyVersion()
[1] "3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)]"
pyOptions("usePandas", TRUE)
pyImport("pandas", as="pd")
test_code <- 'py_df = pd.read_csv("test.csv")'
pyExec(code = test_code)
r_df <- pyGet("py_df")

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""PythonObject"" to a data.frame

Can anyone help to suggest how to convert a Pandas data frame to R data.frame using PythonInR?

4
  • Why not just use R's read.table rather than pursuing such a round about method? Or just stay in Python. There doesn't seem to be any case for synergy here. Commented Aug 15, 2017 at 5:15
  • Thanks for your comment, but I just need to combine R and Python codes in one of the projects as one whole data processing flow, which will require the conversion between Python and R objects Commented Aug 15, 2017 at 5:19
  • Have you heard of the feather project? Commented Aug 15, 2017 at 6:12
  • Thanks Roman for your suggestion, yes feather can be used for both Python and R, but how can I transfer the feather object created in R to my Python code, other than write it as local file and then read into Python? Commented Aug 16, 2017 at 3:57

2 Answers 2

1

You could use json. The pandas dataframe has a to_json method and you can use the rjson R package to read the json string using fromJSON.

You may also find this and this interesting. Planning to submit to CRAN soon, but I need to do more testing.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for your suggestion! From my understanding, I still need to write the json file to local and then read it into R. But I wonder is there a way to convert between R and Python objects that does not require reading and writing? cuz I can already call Python code from R.
@yichi-liu, it depends on your workflow but you can pyExec the json module import and write the to_JSON output to a Python string variable, and transfer that to R with pyGet.
0

it's hard to say why it is not working.

Do you have a reproducible example, or can you provide "test.csv". If you think it's a bug, you could write the package author a mail.

As far I see,

sessionInfo()

## R version 3.4.0 (2017-04-21)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Debian GNU/Linux 8 (jessie)
## 
## Matrix products: default
## BLAS: /home/florian/bin/R_dev/lib/libRblas.so
## LAPACK: /home/florian/bin/R_dev/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=de_AT.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=de_AT.UTF-8        LC_COLLATE=de_AT.UTF-8    
##  [5] LC_MONETARY=de_AT.UTF-8    LC_MESSAGES=de_AT.UTF-8   
##  [7] LC_PAPER=de_AT.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=de_AT.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] knitr_1.16      PythonInR_0.1-3
## 
## loaded via a namespace (and not attached):
## [1] compiler_3.4.0 magrittr_1.5   R6_2.2.1       markdown_0.8  
## [5] tools_3.4.0    stringi_1.1.5  pack_0.1-1     stringr_1.2.0 
## [9] evaluate_0.10

_

library(PythonInR)
pyConnect() 
## R is already connected to Python!

_

pyIsConnected() 
## [1] TRUE

_

pyVersion()
## [1] "2.7.9 (default, Mar  1 2015, 13:01:26) \n[GCC 4.9.2]"

_

pyOptions("usePandas", TRUE) 
pyImport("pandas", as="pd") 

_

pySet("x", head(cars))   
pyExecp("x")
##    dist  speed
## 0     2      4
## 1    10      4
## 2     4      7
## 3    22      7
## 4    16      8
## 5    10      9

_

pyExecp("type(x)")
## <class 'pandas.core.frame.DataFrame'>
str(pyGet("x"))
## 'data.frame':    6 obs. of  2 variables:
##  $ speed: num  4 4 7 7 8 9
##  $ dist : num  2 10 4 22 16 10

1 Comment

It looks like OP opened a bug report on PythonInR Cannot convert Python Pandas Dataframe to R. Based on his example I would guess that it's having trouble converting the name column which contains strings. Strings are not a native pandas data type so it falls back to using a "Python Object" to store those. I barely know pandas and don't have R, but you could test this by rerunning your code using a csv that contains a column of string values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.