<!DOCTYPE html><html><head><title></title><style type="text/css">p.MsoNormal,p.MsoNoSpacing{margin:0}</style></head><body><div>Thanks!  I understand your hesitation; certainly if I am using undocumented APIs (or even if they are otherwise unused) I understand that it will be on my shoulders to preserve compatibility if you change things.  But I would definitely like to get basic functionality working with current (and older, to be honest) NumPy versions.<br></div><div><br></div><div>Ah, I missed previously that the array objects are passed as the void* args to the PyArray_VectorUnaryFunc.  But it's documented here. <br></div><div><a href="https://docs.scipy.org/doc/numpy-1.13.0/reference/c-api.types-and-structures.html#c.PyArray_ArrFuncs.cast">https://docs.scipy.org/doc/numpy-1.13.0/reference/c-api.types-and-structures.html#c.PyArray_ArrFuncs.cast</a><br></div><div>(Why aren't these two args PyArrayObject* then?)<br></div><div><br></div><div>As far as I can tell, get_datetime_metadata_from_dtype() isn't a public API?  It certainly isn't spelled like one.  It's declared in numpy/core/src/multiarray/_datetime.h, which doesn't appear to be #included in any other header.  I suppose I could duplicate the struct layout in my own code to fish out the field I need.  Obviously, compatibility is my own problem then.<br></div><div><br></div><div>Regards,<br></div><div>Alex<br></div><div><br></div><div><br></div><div><br></div><div>On Mon, May 13, 2019, at 01:31, Sebastian Berg wrote:<br></div><blockquote type="cite" id="qt"><div>Hi Alex,<br></div><div><br></div><div>On Mon, 2019-05-13 at 00:35 -0400, Alex Samuel wrote:<br></div><div>> Hi,<br></div><div>> <br></div><div>> When registering a custom cast function from datetime64 to another<br></div><div>> dtype, how can I get the units?<br></div><div>> <br></div><div>> I am calling PyArray_RegisterCastFunc from NPY_DATETIME.  Ideally,<br></div><div>> I'd like to register a separate cast function for each datetime64<br></div><div>> units (or none at all... I don't want all units to be castable). <br></div><div>> Next best thing would be to obtain the units in the cast function and<br></div><div>> dispatch accordingly.<br></div><div>> <br></div><div>> Is this possible?  I glanced through the code, and it looks like<br></div><div>> there's a lot of hard-coded logic around datetime64, but I didn't go<br></div><div>> through it carefully.  Thought I'd ask before drilling further down.<br></div><div>> get_datetime_metadata_from_dtype<br></div><div><br></div><div>No, I do not think that is possible. But you do get the array pointers<br></div><div>during the cast. I honestly would prefer not to promise that all of<br></div><div>this will survive if we change this in numpy, since there are almost no<br></div><div>users. But I think if we change it I could promise to help with<br></div><div>cleaning it up in ora.<br></div><div><br></div><div>I think this is public API (but I do not really like anyone using it<br></div><div>;)), so you can use:<br></div><div><br></div><div>```<br></div><div>/*<br></div><div>* This function returns a pointer to the DateTimeMetaData<br></div><div>* contained within the provided datetime dtype.<br></div><div>*/<br></div><div>static PyArray_DatetimeMetaData *<br></div><div>get_datetime_metadata_from_dtype(PyArray_Descr *dtype)<br></div><div>{<br></div><div>    /* original error check for DATETIME unnecessary for you */<br></div><div>    return &(((PyArray_DatetimeDTypeMetaData *)dtype->c_metadata)->meta);<br></div><div>}<br></div><div>```<br></div><div><br></div><div>And in the castfunc (fromarr is passed in as a void * as far as):<br></div><div>```<br></div><div>NPY_DATETIMEUNIT base = get_datetime_metadata_from_dtype(<br></div><div>        PyArray_DESCR(fromarr))->base;<br></div><div>```<br></div><div><br></div><div>Where NPY_DATETIMEUNIT is the enum defined in ndarraytypes.h. The logic<br></div><div>will have to happen inside the cast func.<br></div><div><br></div><div>I would hope there was a better way, but I cannot think of any, and I<br></div><div>am scared that supporting this will add yet another ugly hack when we<br></div><div>want to improve dtypes...<br></div><div><br></div><div>Best,<br></div><div><br></div><div>Sebastian<br></div><div><br></div><div><br></div><div><br></div><div>> Thanks in advance,<br></div><div>> Alex<br></div><div>> <br></div><div>> <br></div><div>> _______________________________________________<br></div><div>> NumPy-Discussion mailing list<br></div><div>> NumPy-Discussion@python.org<br></div><div>> https://mail.python.org/mailman/listinfo/numpy-discussion<br></div><div><br></div><div>_______________________________________________<br></div><div>NumPy-Discussion mailing list<br></div><div>NumPy-Discussion@python.org<br></div><div>https://mail.python.org/mailman/listinfo/numpy-discussion<br></div><div><br></div><div><br></div><div><b>Attachments:</b><br></div><ul><li>signature.asc<br></li></ul></blockquote><div><br></div></body></html>