我有一个针对Apache Arrow C++库构建的python库,它使用Pybind绑定到python。我希望能够用C++编写一个函数来获取用PyArrow构造的表,例如:
void test(arrow::Table test);
传入PyArrow表,如下所示:
tab = pa.Table.from_pandas(df)
mybinding.test(tab)
如果我像上面那样做一个简单的函数,我会得到:
TypeError: arrow_test(): incompatible function arguments. The following argument types are supported:
1. (arg0: arrow::Table) -> None
Invoked with: pyarrow.Table
我也尝试过编写一个接受py::object
和.cast<arrow::Table>()
参数的函数,但我不能进行强制转换:
RuntimeError: Unable to cast Python instance to C++ type (compile in debug mode for details)
有谁知道怎么让它工作吗?
发布于 2019-09-12 20:45:14
您必须使用arrow/python/pyarrow.h
标头中提供的功能。此标头是自动生成的,以支持将Cython pyarrow.Table
对象展开为C++ arrow::Table
实例。构建并链接到libarrow.so
就足够了。它还需要加载pyarrow
python包,但这只是一个运行时,而不是编译时依赖项。
// header that
#include <arrow/python/pyarrow.h>
// Ensure that the Python module was loaded
arrow::py::import_pyarrow();
PyObject* pyarrow_table = …
// With pybind11 you can also use
// pybind11::object pyarrow_table = …
// Convert PyObject* to native C++ object
std::shared_ptr<Table> table = unwrap_pyarrow_table(pyarrow_table);
https://stackoverflow.com/questions/57863751
复制相似问题