如何在网状py箭头计算表达式中使用筛选器。
在基础上,我有一个pyarrow数据集(在本例中称为woodcan),希望将其转换为带有筛选器的表。
tab <- woodcan$to_table(ds$field('Region')=='Canada')以上所示为Error in py_compare_impl(a, b, op) : ValueError: An Expression cannot be evaluated to python True or False. If you are using the 'and', 'or' or 'not' operators, use '&', '|' or '~' instead.
这个语法看起来怎么样?
发布于 2022-09-15 19:58:49
可以使用py_run_string或py_run_file生成运行python代码的表达式,并将其传递给to_table的filter参数。
library(reticulate)
run.py <- py_run_string('
import pyarrow.dataset as ds
expr = ds.field("Region") == "Canada"
')
woodcan$to_table(filter=run.py$expr)上面的代码需要以前安装py_arrow时使用reticulate。
virtualenv_create("arrow-env")
arrow::install_pyarrow("arrow-env")
use_virtualenv("arrow-env")发布于 2022-09-19 18:04:06
有了@Waldi的回答,我想出了另一种不依赖python字符串的方法。
根据这个答案,我们生成run.py$expr,这是一个<pyarrow.compute.Expression (Region == "Canada")>对象。
这就引出了一个问题,不然我们怎么能产生这样的结果呢?因为它是一个pyarrow.compute.Expression,告诉我们我们需要,当然,pyarrow.compute
因此,它只是将"==“替换为equal
pc <- import('pyarrow.compute')
filter=pc$equal(pc$field("Region"), pc$scalar("Canada"))最后
woodcan$to_table(filter=filter)https://stackoverflow.com/questions/73376668
复制相似问题