Skip to content

Commit

Permalink
BUG: Fix promote_types float32->m8 behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudh2290 committed Jun 19, 2020
1 parent f253a7e commit 18f2008
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
7 changes: 7 additions & 0 deletions doc/release/upcoming_changes/16592.compatibility.rst
@@ -0,0 +1,7 @@
float->timedelta promotion will raise a TypeError
-------------------------------------------------
Float and timedelta promotion consistently raises a TypeError.
``np.promote_types("float32", "m8")`` aligns with
``np.promote_types("m8", "float32")`` now and both raise a TypeError.
Previously, ``np.promote_types("float32", "m8")`` returned ``"m8"`` which
was considered a bug.
3 changes: 1 addition & 2 deletions numpy/core/src/multiarray/convert_datatype.c
Expand Up @@ -1419,8 +1419,7 @@ PyArray_PromoteTypes(PyArray_Descr *type1, PyArray_Descr *type2)
}
break;
case NPY_TIMEDELTA:
if (PyTypeNum_ISINTEGER(type_num1) ||
PyTypeNum_ISFLOAT(type_num1)) {
if (PyTypeNum_ISINTEGER(type_num1)) {
return ensure_dtype_nbo(type2);
}
break;
Expand Down
4 changes: 4 additions & 0 deletions numpy/core/tests/test_datetime.py
Expand Up @@ -775,6 +775,10 @@ def test_dtype_promotion(self):
np.dtype('m8[Y]'), np.dtype('m8[D]'))
assert_raises(TypeError, np.promote_types,
np.dtype('m8[M]'), np.dtype('m8[W]'))
# timedelta and float cannot be safely cast with each other
assert_raises(TypeError, np.promote_types, "float32", "m8")
assert_raises(TypeError, np.promote_types, "m8", "float32")

# timedelta <op> timedelta may overflow with big unit ranges
assert_raises(OverflowError, np.promote_types,
np.dtype('m8[W]'), np.dtype('m8[fs]'))
Expand Down

0 comments on commit 18f2008

Please sign in to comment.