MCPcopy Create free account
hub / github.com/numpy/numpy / PyArray_InitializeDatetimeCasts

Function PyArray_InitializeDatetimeCasts

numpy/core/src/multiarray/datetime.c:4085–4284  ·  view source on GitHub ↗

* This registers the castingimpl for all datetime related casts. */

Source from the content-addressed store, hash-verified

4083 * This registers the castingimpl for all datetime related casts.
4084 */
4085NPY_NO_EXPORT int
4086PyArray_InitializeDatetimeCasts()
4087{
4088 int result = -1;
4089
4090 PyType_Slot slots[3];
4091 PyArray_DTypeMeta *dtypes[2];
4092 PyArrayMethod_Spec spec = {
4093 .name = "datetime_casts",
4094 .nin = 1,
4095 .nout = 1,
4096 .casting = NPY_UNSAFE_CASTING,
4097 .flags = NPY_METH_SUPPORTS_UNALIGNED,
4098 .dtypes = dtypes,
4099 .slots = slots,
4100 };
4101 slots[0].slot = NPY_METH_resolve_descriptors;
4102 slots[0].pfunc = &time_to_time_resolve_descriptors;
4103 slots[1].slot = _NPY_METH_get_loop;
4104 slots[1].pfunc = &time_to_time_get_loop;
4105 slots[2].slot = 0;
4106 slots[2].pfunc = NULL;
4107
4108 PyArray_DTypeMeta *datetime = PyArray_DTypeFromTypeNum(NPY_DATETIME);
4109 PyArray_DTypeMeta *timedelta = PyArray_DTypeFromTypeNum(NPY_TIMEDELTA);
4110 PyArray_DTypeMeta *string = PyArray_DTypeFromTypeNum(NPY_STRING);
4111 PyArray_DTypeMeta *unicode = PyArray_DTypeFromTypeNum(NPY_UNICODE);
4112 PyArray_DTypeMeta *tmp = NULL;
4113
4114 dtypes[0] = datetime;
4115 dtypes[1] = datetime;
4116 if (PyArray_AddCastingImplementation_FromSpec(&spec, 1) < 0) {
4117 goto fail;
4118 }
4119 dtypes[0] = timedelta;
4120 dtypes[1] = timedelta;
4121 if (PyArray_AddCastingImplementation_FromSpec(&spec, 1) < 0) {
4122 goto fail;
4123 }
4124
4125 /*
4126 * Casting between timedelta and datetime uses legacy casting loops, but
4127 * custom dtype resolution (to handle copying of the time unit).
4128 */
4129 spec.flags = NPY_METH_REQUIRES_PYAPI;
4130
4131 slots[0].slot = NPY_METH_resolve_descriptors;
4132 slots[0].pfunc = &datetime_to_timedelta_resolve_descriptors;
4133 slots[1].slot = _NPY_METH_get_loop;
4134 slots[1].pfunc = &legacy_cast_get_strided_loop;
4135 slots[2].slot = 0;
4136 slots[2].pfunc = NULL;
4137
4138 spec.name = "timedelta_and_datetime_cast";
4139 dtypes[0] = timedelta;
4140 dtypes[1] = datetime;
4141 if (PyArray_AddCastingImplementation_FromSpec(&spec, 1) < 0) {
4142 goto fail;

Callers 1

PyArray_InitializeCastsFunction · 0.85

Tested by

no test coverage detected