[Numpy-discussion] casting from datetime64
sebastian at sipsolutions.net
Mon May 13 01:29:59 EDT 2019
On Mon, 2019-05-13 at 00:35 -0400, Alex Samuel wrote:
> 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 *
/* 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,
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 833 bytes
Desc: This is a digitally signed message part
More information about the NumPy-Discussion