pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

Passing numpy array containing strings

Open mnovosad1095 opened this issue 4 years ago • 3 comments

Issue description

Hi, I'm trying to write a code, which will receive the np.array containing strings, do some logic with them, and then return it back. The code worked perfectly, while I was passing numeric values. It compiles but I get the following error: RuntimeError: NumPy type info missing for PKc

The interesting thing that bothers me that if I fill a vector with some random strings it will convert it and return it without any problems

This is my C++ code:

#include <sstream>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <stdlib.h> 
#include <cuda_runtime.h>

namespace py = pybind11;

py::array do_stuff(py::array_t<const char*> input) {
  std::vector<std::string> vec;
  py::buffer_info info = input.request();

  for (ssize_t i = 0; i < info.shape[0]; ++i) {
    vec.emplace_back(*input.data(i));
  }

  return py::array(py::cast(vec));
}


PYBIND11_MODULE(jam_lib, m) {
  // m.def("run", &run);
  m.def("do_stuff", &do_stuff);

#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}```

mnovosad1095 avatar May 22 '20 23:05 mnovosad1095

Hi There,

is there any progress regarding this topic? I`m facing the same issue currently and would like to know what would be the best/fastest solution to hand-over numpy string array to C++.

Cheers

DonIvanCorleone avatar Nov 25 '21 11:11 DonIvanCorleone

You may want try the arrow string array. Arrow can zero copy to numpy. some example like: https://github.com/rewreu/arrow-ext/blob/master/src/ext.cpp

rewreu avatar Mar 18 '22 03:03 rewreu

Sorry for the delayed response @rewreu, thats the way we did it eventually. Many thanks for your input.

DonIvanCorleone avatar Jul 27 '22 09:07 DonIvanCorleone