pybind11
pybind11 copied to clipboard
Passing numpy array containing strings
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
}```
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
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
Sorry for the delayed response @rewreu, thats the way we did it eventually. Many thanks for your input.