pybind / pybind/pybind11

Convert vector of string to numpy array

Open
#1,775 4 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

If the max length of string is known in advance, I can write code like:

std::vector<std::string> v = {"1", "23", "456"};
using S8 = char[8];
py::array_t<S8> arr(v.size());
auto pdest = static_cast<S8*>(arr.request().ptr);
for (std::size_t i = 0; i < v.size(); ++i) {
  std::strncpy(pdest[i], v[i].data(), 8);
}

But what if the vector is created dynamically and the longest string is unknown? Please advise.

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

No repository file or test is named. Start by reviewing the C++ example and the pybind11 NumPy-array conversion entry points for std::vectorstd::string; determine the supported behavior for dynamically sized strings, then add coverage that demonstrates the resulting conversion.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, numpy, python
Domain
api, data
Issue type
Feature
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.