pybind / pybind/pybind11

Passing numpy array containing strings

Open
#2,226 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

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
}```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided do_stuff entry point with a NumPy array containing strings, focusing on py::array_t<const char*> and input.request(). Check the pybind11 NumPy conversion behavior around the reported “NumPy type info missing for PKc” error. Done means the supported behavior or limitation is established and the issue has a clear resolution.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, numpy, python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.