[Numpy-discussion] casting from datetime64

Sebastian Berg sebastian at sipsolutions.net
Mon May 13 01:29:59 EDT 2019

Hi Alex,

On Mon, 2019-05-13 at 00:35 -0400, Alex Samuel wrote:
> Hi,
> When registering a custom cast function from datetime64 to another
> dtype, how can I get the units?
> I am calling PyArray_RegisterCastFunc from NPY_DATETIME.  Ideally,
> I'd like to register a separate cast function for each datetime64
> units (or none at all... I don't want all units to be castable). 
> Next best thing would be to obtain the units in the cast function and
> dispatch accordingly.
> Is this possible?  I glanced through the code, and it looks like
> there's a lot of hard-coded logic around datetime64, but I didn't go
> through it carefully.  Thought I'd ask before drilling further down.

No, I do not think that is possible. But you do get the array pointers
during the cast. I honestly would prefer not to promise that all of
this will survive if we change this in numpy, since there are almost no
users. But I think if we change it I could promise to help with
cleaning it up in ora.

I think this is public API (but I do not really like anyone using it
;)), so you can use:

 * This function returns a pointer to the DateTimeMetaData
 * contained within the provided datetime dtype.
static PyArray_DatetimeMetaData *
get_datetime_metadata_from_dtype(PyArray_Descr *dtype)
    /* original error check for DATETIME unnecessary for you */
    return &(((PyArray_DatetimeDTypeMetaData *)dtype->c_metadata)->meta);

And in the castfunc (fromarr is passed in as a void * as far as):
NPY_DATETIMEUNIT base = get_datetime_metadata_from_dtype(

Where NPY_DATETIMEUNIT is the enum defined in ndarraytypes.h. The logic
will have to happen inside the cast func.

I would hope there was a better way, but I cannot think of any, and I
am scared that supporting this will add yet another ugly hack when we
want to improve dtypes...



> Thanks in advance,
> Alex
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20190512/5bf833ed/attachment.sig>

More information about the NumPy-Discussion mailing list