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

Function convert_pyobjects_to_datetimes

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

* Converts an array of PyObject * into datetimes and/or timedeltas, * based on the values in type_nums. * * If inout_meta->base is -1, uses GCDs to calculate the metadata, filling * in 'inout_meta' with the resulting metadata. Otherwise uses the provided * 'inout_meta' for all the conversions. * * When obj[i] is NULL, out_value[i] will be set to NPY_DATETIME_NAT. * * Returns 0 on success,

Source from the content-addressed store, hash-verified

3129 * Returns 0 on success, -1 on failure.
3130 */
3131NPY_NO_EXPORT int
3132convert_pyobjects_to_datetimes(int count,
3133 PyObject **objs, const int *type_nums,
3134 NPY_CASTING casting,
3135 npy_int64 *out_values,
3136 PyArray_DatetimeMetaData *inout_meta)
3137{
3138 int i, is_out_strict;
3139 PyArray_DatetimeMetaData *meta;
3140
3141 /* No values trivially succeeds */
3142 if (count == 0) {
3143 return 0;
3144 }
3145
3146 /* Use the inputs to resolve the unit metadata if requested */
3147 if (inout_meta->base == NPY_FR_ERROR) {
3148 /* Allocate an array of metadata corresponding to the objects */
3149 meta = PyArray_malloc(count * sizeof(PyArray_DatetimeMetaData));
3150 if (meta == NULL) {
3151 PyErr_NoMemory();
3152 return -1;
3153 }
3154
3155 /* Convert all the objects into timedeltas or datetimes */
3156 for (i = 0; i < count; ++i) {
3157 meta[i].base = NPY_FR_ERROR;
3158 meta[i].num = 1;
3159
3160 /* NULL -> NaT */
3161 if (objs[i] == NULL) {
3162 out_values[i] = NPY_DATETIME_NAT;
3163 meta[i].base = NPY_FR_GENERIC;
3164 }
3165 else if (type_nums[i] == NPY_DATETIME) {
3166 if (convert_pyobject_to_datetime(&meta[i], objs[i],
3167 casting, &out_values[i]) < 0) {
3168 PyArray_free(meta);
3169 return -1;
3170 }
3171 }
3172 else if (type_nums[i] == NPY_TIMEDELTA) {
3173 if (convert_pyobject_to_timedelta(&meta[i], objs[i],
3174 casting, &out_values[i]) < 0) {
3175 PyArray_free(meta);
3176 return -1;
3177 }
3178 }
3179 else {
3180 PyErr_SetString(PyExc_ValueError,
3181 "convert_pyobjects_to_datetimes requires that "
3182 "all the type_nums provided be datetime or timedelta");
3183 PyArray_free(meta);
3184 return -1;
3185 }
3186 }
3187
3188 /* Merge all the metadatas, starting with the first one */

Callers 1

datetime_arangeFunction · 0.85

Tested by

no test coverage detected