【问题标题】:Pandas DataFrame to Reticulate results in IndexErrorPandas DataFrame to Reticulate 导致 IndexError
【发布时间】:2019-03-01 19:46:23
【问题描述】:

我有一个用 Python 3.7 编写的函数,它返回一个 Pandas DataFrame。示例:

import pandas

df = pandas.DataFrame({'foo':[1,2,3], 'bar':['one', 'two', 'three'], 'baz':['apple', 'banana', 'strawberry']})

def returnMyDF():

    return df

这个 Python 文件可能被称为 my_dataframe.py

然后在 R 中,我使用 Reticulate 库和 Tidyverse 将 Pandas DataFrame 插入到 Tibble 中。

执行此操作的代码位于 app.R 中,如下所示:

library(tidyverse)
library(reticulate)

use_python("C:/ProgramData/Anaconda3", required = TRUE) 
source_python("C:/the/path/to/my_dataframe.py")

df = returnMyDF()
glimpse(df)

返回以下错误:

Observations: 3 Error in py_call_impl(callable, dots$args, dots$keywords) : IndexError: index 3 is out of bounds for axis 0 with size 3

一些事实: 我在 GitHub 中发现了这个问题:https://github.com/rstudio/reticulate/issues/101,我认为它可能会解决。使用devtools::install_github("rstudio/reticulate")更新到最新版本的网状网络

会话信息:

sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  
LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 
LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] forcats_0.3.0   stringr_1.4.0   dplyr_0.7.8     purrr_0.3.0     readr_1.3.1     tidyr_0.8.2     tibble_2.0.1   
[8] ggplot2_3.1.0   tidyverse_1.2.1 reticulate_1.10

loaded via a namespace (and not attached):
[1] Rcpp_1.0.0       cellranger_1.1.0 pillar_1.3.1     compiler_3.5.2   plyr_1.8.4       bindr_0.1.1     
[7] tools_3.5.2      lubridate_1.7.4  jsonlite_1.6     nlme_3.1-137     gtable_0.2.0     lattice_0.20-38 
[13] pkgconfig_2.0.2  rlang_0.3.1      Matrix_1.2-15    cli_1.0.1        rstudioapi_0.9.0 yaml_2.2.0      
[19] haven_2.0.0      bindrcpp_0.2.2   withr_2.1.2      xml2_1.2.0       httr_1.4.0       hms_0.4.2       
[25] generics_0.0.2   grid_3.5.2       tidyselect_0.2.5 glue_1.3.0       R6_2.3.0         readxl_1.2.0    
[31] modelr_0.1.3     magrittr_1.5     backports_1.1.3  scales_1.0.0     rvest_0.3.2      assertthat_0.2.0
[37] colorspace_1.4-0 stringi_1.2.4    lazyeval_0.2.1   munsell_0.5.0    broom_0.5.1      crayon_1.3.4`   

为了检查 NumPy 是否有效,我可以更改 my_dataframe.py(并适当地更改 app.R) 并导入一个 NumPy 数组......这不会导致任何问题:

import numpy

my_array = numpy.array([42, 2.38, 42])

def returnMyArray():

    return my_array

我的问题是:如何将 Pandas DataFrame 引入 R 等效项?

【问题讨论】:

  • 好吧,对不起,但我不知道 R,你能发布一个预期的结果并解释你想如何得到它。对于只有 python/pandas 背景的人来说会更容易
  • 我相信这在 R 中被称为 tibble。所以预期的结果是 tibble。具有 R 知识的人可能知道 reticulate + tidyverse 创建的不同对象。
  • 好吧。 :) 这是我的建议,因为我不知道 R。
  • 当然。谢谢!

标签: python r pandas tidyverse reticulate


【解决方案1】:

我主要是 R 用户并开始涉足 python,但一个可能的解决方案可能是在 Rmarkdown 中编写你的 python 代码。您可以在此处交替编写 pythonr 代码 - 这是一个很好的入门资源 https://cran.r-project.org/web/packages/reticulate/vignettes/r_markdown.html

如果您不熟悉 r markdown,我可以提供更多信息。

---
title: "test"
output: html_document
---

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = TRUE)
#engine to run python
library(reticulate)

```



```{python}
#python code R knows this is python code because you specified 
# this above  "```{python}"

import pandas

df = pandas.DataFrame({'foo':[1,2,3], 'bar':['one', 'two', 'three'], 'baz':['apple', 'banana', 'strawberry']})

print(df)

```



```{r}
#r code 
#refer to get python objects in R code you have to type py$objectname
df2 <- py$df
class(df2)
#data.frame - python equivalent to pandas.DataFrame
```

【讨论】:

  • 只是通过这个来讨论,因为我比 R 更 Python,但是:拥有 app.R 文件的原因是因为 app.R 是一个闪亮的脚本。我可以将我所有的代码从 app.R 脚本移动到 R Markdown 中,然后执行上述操作吗?
  • @kevcisme 抱歉,我不知道这是针对闪亮的应用程序。我认为最好不要把它带入rmarkdown。我会看看是否有一种方法可以将 pandas 数据框调用到 r 脚本中,而无需移动到 markdown。
  • @kevcisme 在你的 python 脚本中你可以删除 def returnMyDF(): return df 吗?我能够在没有这些脚本的情况下调用 python 脚本,并且能够使用 df 对象。还要确保您的网状网络版本是 1.11.1 或更高版本
  • 啊不幸的是没有。该 returnMyDF() 函数是我使用 Python 的目的。我在 Python 中进行了一些建模,然后将结果输出到 Pandas 数据框,然后尝试将该数据框传递给 R,以便我可以显示为闪亮的仪表板。
  • df = pd.DataFrame(...) 只是为了有人可以重现错误。
猜你喜欢
  • 1970-01-01
  • 2016-03-27
  • 2021-12-25
  • 2020-12-04
  • 1970-01-01
  • 2017-04-10
  • 2021-08-17
  • 2018-01-23
  • 1970-01-01
相关资源
最近更新 更多