通过C ++ API将Python序列转换为箭头数组

时间:2019-06-04 14:08:23

标签: pyarrow apache-arrow

我正在尝试研究Arrow如何使用下面的C ++ API将python列表转换为等效的arrow::Array

#include <memory>
#include <Python.h>
#include <string>
#include <iostream>
#include <arrow/memory_pool.h>
#include <arrow/python/python_to_arrow.h>


PyObject* clist(void)
{
    PyObject* lst = PyList_New(0);
    PyList_Append(lst, PyLong_FromLong(1));
    PyList_Append(lst, PyLong_FromLong(2));
    PyList_Append(lst, PyLong_FromLong(5));

    return lst;
}

int main()
{
    Py_Initialize();

    PyObject* list = clist();

    std::shared_ptr<arrow::ChunkedArray> carr;

    arrow::py::PyConversionOptions ops;
    ops.from_pandas = false;
    ops.pool = arrow::default_memory_pool(); 
    ConvertPySequence(list, ops, &carr);

    Py_Finalize();

}

文件编译正常,但是在arrow/cpp/src/arrow/python/iterators.h的{​​{1}}第44行出现了分段错误。

我的调试器中的错误是PyCheck_Array,但是当我在调试控制台中查询它时,它似乎出现在内存中:

enter image description here

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您需要通过调用arrow_init_numpy()来初始化NumPy C API。见

https://github.com/apache/arrow/blob/master/cpp/src/arrow/python/util/test_main.cc#L24