Hello Sebastian,
Thanks for detail.
The last call has the NPY_ARRAY_C_CONTIGUOUS and NPY_ARRAY_ALIGNED,
thus I guess it can be no-op most of the time but for some unknown case
it's may be safer ?
Best regards
On Tue, 2022-01-18 at 09:22 -0600, Sebastian Berg wrote:
> On Tue, 2022-01-18 at 14:56 +0100, Benoit Gschwind wrote:
> > Hello,
> >
> > I using the following code:
> >
> > if (PyArray_TYPE(arr1) == NPY_DATETIME) {
> > // Ensure datetime64[ms]
> > auto tmp =
> > reinterpret_cast<PyArrayObject*>(PyObject_CallMethod(reinterpret_ca
> > st
> > <PyObject*>(arr1), "astype", "(s)", "datetime64[ms]"));
> > std::swap(arr1, tmp);
> > Py_XDECREF(tmp);
> > // Ensure integer
> > tmp =
> > reinterpret_cast<PyArrayObject*>(PyObject_CallMethod(reinterpret_ca
> > st
> > <PyObject*>(arr1), "astype", "(s)", "i8"));
> > std::swap(arr1, tmp);
> > Py_XDECREF(tmp);
> > tmp =
> > reinterpret_cast<PyArrayObject*>(PyArray_FromArray(arr1,
> > PyArray_DescrFromType(NPY_INT64), NPY_ARRAY_IN_ARRAY));
> > std::swap(arr1, tmp);
> > Py_XDECREF(tmp);
> > }
> >
> > First, if something is wrong with my code let me known. Then I
> > wonder
> > if I can have a safe shortcut to avoid converting datetime64 to i8.
> > I
> > guess the internal data of datetime64[ms] is i8 thus copying and
> > casting the array may be avoided.
>
> Yes, you can assume datetime64 is stored as an i8 with the unit and
> possible byteswapping. Both of which, your initial cast will ensure.
>
> The internal data is i8, except for the special NaT value.
>
> Code-wise, you can avoid calling `astype`, but if you do (also in
> python), I suggest to pass `copy=False`, so that it does not copy if
> it
> clearly is not necessary (I am hoping to improve on the "clearly"
> here
> at some point).
>
> The last call again seems to be a no-op? Just the last call with the
> correct `datetime64[ms]` descriptor could be enough.
>
> Cheers,
>
> Sebastian
>
>
> >
> > Thanks by advance
> > Best regards.
> >
> >
> > _______________________________________________
> > NumPy-Discussion mailing list -- [email protected]
> > To unsubscribe send an email to [email protected]
> > https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
> > Member address: [email protected]
> >
>
> _______________________________________________
> NumPy-Discussion mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
> Member address: [email protected]
_______________________________________________
NumPy-Discussion mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: [email protected]