8000 Merge pull request #16592 from anirudh2290/fix_timedelta_promotion · numpy/numpy@2f156d8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f156d8

Browse files
authored
Merge pull request #16592 from anirudh2290/fix_timedelta_promotion
BUG: Raise TypeError for float->timedelta promotion
2 parents f253a7e + 18f2008 commit 2f156d8

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
float->timedelta promotion will raise a TypeError
2+
-------------------------------------------------
3+
Float and timedelta promotion consistently raises a TypeError.
4+
``np.promote_types("float32", "m8")`` aligns with
5+
``np.promote_types("m8", "float32")`` now and both raise a TypeError.
6+
Previously, ``np.promote_types("float32", "m8")`` returned ``"m8"`` which
7+
was considered a bug.

numpy/core/src/multiarray/convert_datatype.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,8 +1419,7 @@ PyArray_PromoteTypes(PyArray_Descr *type1, PyA C3F9 rray_Descr *type2)
14191419
}
14201420
break;
14211421
case NPY_TIMEDELTA:
1422-
if (PyTypeNum_ISINTEGER(type_num1) ||
1423-
PyTypeNum_ISFLOAT(type_num1)) {
1422+
if (PyTypeNum_ISINTEGER(type_num1)) {
14241423
return ensure_dtype_nbo(type2);
14251424
}
14261425
break;

numpy/core/tests/test_datetime.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,10 @@ def test_dtype_promotion(self):
775775
np.dtype('m8[Y]'), np.dtype('m8[D]'))
776776
assert_raises(TypeError, np.promote_types,
777777
np.dtype('m8[M]'), np.dtype('m8[W]'))
778+
# timedelta and float cannot be safely cast with each other
779+
assert_raises(TypeError, np.promote_types, "float32", "m8")
780+
assert_raises(TypeError, np.promote_types, "m8", "float32")
781+
778782
# timedelta <op> timedelta may overflow with big unit ranges
779783
assert_raises(OverflowError, np.promote_types,
780784
np.dtype('m8[W]'), np.dtype('m8[fs]'))

0 commit comments

Comments
 (0)
0