Reputation: 23
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
?
Upvotes: 2
Views: 4024
Reputation: 663
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
Upvotes: 0