Skip to content Skip to sidebar Skip to footer

How To Create Of Numpy Array Of Datetime64 Objects Using C API?

I need to create an array of numpy datetime64 objects from C/C++ code. As you can see for NPY_LONGLONG and NPY_VOID I did it. I need to do the same thing for NPY_DATETIME type. Py

Solution 1:

I found a good solution. Here is my function that creates numpy array from a C buffer.

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 is M8[ms] or M8[us] or M8[ns].


Post a Comment for "How To Create Of Numpy Array Of Datetime64 Objects Using C API?"