我需要从 C/C++ 代码创建 numpy datetime64 对象的数组。正如你所看到的NPY_LONGLONG
and NPY_VOID
我做到了。我需要做同样的事情NPY_DATETIME
type.
PyObject *arr1 = PyArray_SimpleNew(1, &dims, NPY_LONGLONG);
PyObject *arr2 = PyArray_New(&PyArray_Type, 1, &dims, NPY_VOID, NULL, NULL, item_size, 0, NULL);
问题是没有关于内部表示是什么的文档NPY_DATETIME
类型,所以我不知道它是否有固定的大小、结构。
如果你能像我一样举一个例子,那就太好了NPY_LONGLONG
and NPY_VOID
.
我找到了一个很好的解决方案。这是我从 C 缓冲区创建 numpy 数组的函数。
PyObject* create_datetime_array(int index, std::string const &dtype)
{
int buffer_size = this->elements_count*sizeof(omd::OT_int64);
npy_intp dims = this->elements_count;
PyObject *date_type = Py_BuildValue("s", dtype.c_str());
PyArray_Descr *descr;
PyArray_DescrConverter(date_type, &descr);
Py_XDECREF(date_type);
PyObject *arr = PyArray_SimpleNewFromDescr(1, &dims, descr);
memcpy(PyArray_BYTES((PyArrayObject *)arr), &(this->int64_data[index][0]), buffer_size);
return arr;
}
dtype
是 M8[ms] 或 M8[us] 或 M8[ns]。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)