pyarrow Table to PyObject* via pybind11

  apache-arrow, c++, pyarrow, pybind11
#include <pybind11/pybind11.h>
#include <Python.h>
#include <iostream>
#include <arrow/python/pyarrow.h>
#include <arrow/table.h>

// Convert pyarrow table to native C++ object and print its contents
void print_table(PyObject* py_table)
{
    // convert pyobject to table
    auto status = arrow::py::unwrap_table(py_table);
    if (!status.ok())
    {
        std::cout << "Error converting pyarrow table to arrow table" << std::endl;
        return;
    }
    std::shared_ptr<arrow::Table> table = status.ValueOrDie();
    std::cout << "Table has " << table->num_rows() << " rows" << std::endl;
    // print table
    for (int i = 0; i < table->num_columns(); i++)
    {
        std::cout << "Column " << i << ": " << table->column(i)->ToString() << std::endl;
    }
}

PYBIND11_MODULE(df_test, m)
{
    arrow::py::import_pyarrow();
    m.doc() = "Pyarrow Extensions";
    m.def("print_table", &print_table);
}

I’m able to successfully build a c++ library via pybind11 which accepts a PyObject* and hopefully prints the contents of a pyarrow table passed to it. However, after converting my pandas.DataFrame to a pyarrow.Table as follows,

# convert to pyarrow table
table = pa.Table.from_pandas(df)

import df_test
df_test.print_table(table)

the code complains that

TypeError: print_table(): incompatible function arguments. The following argument types are supported:
    1. (arg0: _object) -> None

How do I convert the pyarrow.Table to a PyObject*?

Source: Windows Questions C++

LEAVE A COMMENT