summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2019-10-15 17:56:35 -0700
committerEric Wieser <wieser.eric@gmail.com>2019-10-16 09:35:53 +0100
commitac757b50edbbdb49cf0c1804d7f6273e16d269a4 (patch)
treef2b11380e78d84552b9c1b417e1c844a9fdf7007
parent30b2b587d0b33d53cab72b11cb8da5378f277a30 (diff)
downloadnumpy-ac757b50edbbdb49cf0c1804d7f6273e16d269a4.tar.gz
Followup: Make "same_kind" casting possible for consistency
-rw-r--r--doc/release/upcoming_changes/14718.compatibility.rst1
-rw-r--r--numpy/core/src/multiarray/convert_datatype.c8
-rw-r--r--numpy/core/tests/test_datetime.py2
3 files changed, 9 insertions, 2 deletions
diff --git a/doc/release/upcoming_changes/14718.compatibility.rst b/doc/release/upcoming_changes/14718.compatibility.rst
index a42a2b388..3d6e71ead 100644
--- a/doc/release/upcoming_changes/14718.compatibility.rst
+++ b/doc/release/upcoming_changes/14718.compatibility.rst
@@ -1,5 +1,6 @@
``np.can_cast(np.uint64, np.timedelta64, casting='safe')`` is now ``False``
---------------------------------------------------------------------------
+
Previously this was ``True`` - however, this was inconsistent with ``uint64``
not being safely castable to ``int64``, and resulting in strange type
resolution.
diff --git a/numpy/core/src/multiarray/convert_datatype.c b/numpy/core/src/multiarray/convert_datatype.c
index 025c66013..4326448dc 100644
--- a/numpy/core/src/multiarray/convert_datatype.c
+++ b/numpy/core/src/multiarray/convert_datatype.c
@@ -877,7 +877,13 @@ PyArray_CanCastTypeTo(PyArray_Descr *from, PyArray_Descr *to,
from_order = dtype_kind_to_ordering(from->kind);
to_order = dtype_kind_to_ordering(to->kind);
- return from_order != -1 && from_order <= to_order;
+ if (to->kind == 'm') {
+ /* both types being timedelta is already handled before. */
+ int integer_order = dtype_kind_to_ordering('i');
+ return (from_order != -1) && (from_order <= integer_order);
+ }
+
+ return (from_order != -1) && (from_order <= to_order);
}
else {
return 0;
diff --git a/numpy/core/tests/test_datetime.py b/numpy/core/tests/test_datetime.py
index 71f579525..c34f09f36 100644
--- a/numpy/core/tests/test_datetime.py
+++ b/numpy/core/tests/test_datetime.py
@@ -82,7 +82,7 @@ class TestDateTime(object):
# Cannot cast safely from unsigned integer of the same size, which
# could overflow
- assert_(not np.can_cast('u8', 'm8', casting='same_kind'))
+ assert_(np.can_cast('u8', 'm8', casting='same_kind'))
assert_(not np.can_cast('u8', 'm8', casting='safe'))
# Cannot cast safely/same_kind from float to timedelta