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

Function can_cast_timedelta64_units

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

* This provides the casting rules for the TIMEDELTA data type units. * * Notably, there is a barrier between the nonlinear years and * months units, and all the other units. */

Source from the content-addressed store, hash-verified

1340 * months units, and all the other units.
1341 */
1342NPY_NO_EXPORT npy_bool
1343can_cast_timedelta64_units(NPY_DATETIMEUNIT src_unit,
1344 NPY_DATETIMEUNIT dst_unit,
1345 NPY_CASTING casting)
1346{
1347 switch (casting) {
1348 /* Allow anything with unsafe casting */
1349 case NPY_UNSAFE_CASTING:
1350 return 1;
1351
1352 /*
1353 * Only enforce the 'date units' vs 'time units' barrier with
1354 * 'same_kind' casting.
1355 */
1356 case NPY_SAME_KIND_CASTING:
1357 if (src_unit == NPY_FR_GENERIC || dst_unit == NPY_FR_GENERIC) {
1358 return src_unit == NPY_FR_GENERIC;
1359 }
1360 else {
1361 return (src_unit <= NPY_FR_M && dst_unit <= NPY_FR_M) ||
1362 (src_unit > NPY_FR_M && dst_unit > NPY_FR_M);
1363 }
1364
1365 /*
1366 * Enforce the 'date units' vs 'time units' barrier and that
1367 * casting is only allowed towards more precise units with
1368 * 'safe' casting.
1369 */
1370 case NPY_SAFE_CASTING:
1371 if (src_unit == NPY_FR_GENERIC || dst_unit == NPY_FR_GENERIC) {
1372 return src_unit == NPY_FR_GENERIC;
1373 }
1374 else {
1375 return (src_unit <= dst_unit) &&
1376 ((src_unit <= NPY_FR_M && dst_unit <= NPY_FR_M) ||
1377 (src_unit > NPY_FR_M && dst_unit > NPY_FR_M));
1378 }
1379
1380 /* Enforce equality with 'no' or 'equiv' casting */
1381 default:
1382 return src_unit == dst_unit;
1383 }
1384}
1385
1386/*
1387 * This provides the casting rules for the DATETIME data type metadata.

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected