Issue
I am trying to pass an array of datetime64[ns]
into C++ using pybind11. For arrays of integers or floating point values, one can simply use the wrapper py::array_t<int64_t or double>
.
Is there a dedicated type in pybind11 for datetime64[ns]
in C++ side so that I can capture as py::array_t<DateTime>
?
As a sub-optimal solution, it would be already a great improvement if I can instead pass the underlying storage of a datetime64[ns]
array which is expected to be stored as an array of int64. Is there any light way (i.e., no copy) to pass this as an array of int64?
Solution
It's not the most ergonomic, but you could probably use py::array_t<int64_t>
as the pybind11 interface's type, and convert the array (without copy) like this:
In [1]: a = np.array([np.datetime64(x, 'ns') for x in range(10)])
In [2]: v = a.view(dtype=np.int64)
In [3]: v
Out[3]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64)
In [4]: some_pybind_method(v)
There's a way to write a custom type conversion that does this transparently, but I'm not familiar with the required Python C-API needed for this.
Answered By - unddoch
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.