Skip to content

Commit 93e2ce2

Browse files
committed
[ENH] Raise RuntimeWarning when casting from a unitless np.timedelta64 to unitful one.
This is a temporary solution to handle numpy#28287. Rasing `RuntimeWarning` encourages users to pay attention to the behavior.
1 parent 37c9cb6 commit 93e2ce2

4 files changed

Lines changed: 23 additions & 9 deletions

File tree

‎numpy/_core/src/multiarray/datetime.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,11 @@ get_datetime_conversion_factor(PyArray_DatetimeMetaData *src_meta,
10361036

10371037
/* Generic units change to the destination with no conversion factor */
10381038
if (src_meta->base == NPY_FR_GENERIC) {
1039+
PyErr_WarnEx(PyExc_RuntimeWarning,
1040+
"Casting from unitless timedelta to unitful timedelta is "
1041+
"ambiguous.",
1042+
1);
1043+
10391044
*out_num = 1;
10401045
*out_denom = 1;
10411046
return;
@@ -3887,9 +3892,6 @@ time_to_time_resolve_descriptors(
38873892
return NPY_EQUIV_CASTING;
38883893
}
38893894
else if (meta1->base == NPY_FR_GENERIC) {
3890-
if (byteorder_may_allow_view) {
3891-
*view_offset = 0;
3892-
}
38933895
return NPY_SAFE_CASTING ;
38943896
}
38953897
else if (meta2->base == NPY_FR_GENERIC) {

‎numpy/_core/tests/test_casting_unittests.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def test_numeric_to_times(self, from_Dt):
402402
[("M8[ns]", None, Casting.no, 0, 1, 1),
403403
(str(np.dtype("M8[ns]").newbyteorder()), None,
404404
Casting.equiv, None, 1, 1),
405-
("M8", "M8[ms]", Casting.safe, 0, 1, 1),
405+
("M8", "M8[ms]", Casting.safe, None, 1, 1),
406406
# should be invalid cast:
407407
("M8[ms]", "M8", Casting.unsafe, None, 1, 1),
408408
("M8[5ms]", "M8[5ms]", Casting.no, 0, 1, 1),
@@ -415,7 +415,7 @@ def test_numeric_to_times(self, from_Dt):
415415
("m8[ns]", None, Casting.no, 0, 1, 1),
416416
(str(np.dtype("m8[ns]").newbyteorder()), None,
417417
Casting.equiv, None, 1, 1),
418-
("m8", "m8[ms]", Casting.safe, 0, 1, 1),
418+
("m8", "m8[ms]", Casting.safe, None, 1, 1),
419419
# should be invalid cast:
420420
("m8[ms]", "m8", Casting.unsafe, None, 1, 1),
421421
("m8[5ms]", "m8[5ms]", Casting.no, 0, 1, 1),

‎numpy/lib/tests/test_nanfunctions.py‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -951,21 +951,31 @@ def test_allnans(self, dtype, axis):
951951
assert output.dtype == mat.dtype
952952
assert np.isnan(output).all()
953953

954+
_filtered_record = [
955+
item.message
956+
for item in r
957+
if "All-NaN slice encountered" in str(item.message)
958+
]
954959
if axis is None:
955-
assert_(len(r) == 1)
960+
assert_(len(_filtered_record) == 1)
956961
else:
957-
assert_(len(r) == 3)
962+
assert_(len(_filtered_record) == 3)
958963

959964
# Check scalar
960965
scalar = np.array(np.nan).astype(dtype)[()]
961966
output_scalar = np.nanmedian(scalar)
962967
assert output_scalar.dtype == scalar.dtype
963968
assert np.isnan(output_scalar)
964969

970+
_filtered_record = [
971+
item.message
972+
for item in r
973+
if "All-NaN slice encountered" in str(item.message)
974+
]
965975
if axis is None:
966-
assert_(len(r) == 2)
976+
assert_(len(_filtered_record) == 2)
967977
else:
968-
assert_(len(r) == 4)
978+
assert_(len(_filtered_record) == 4)
969979

970980
def test_empty(self):
971981
mat = np.zeros((0, 3))

‎pytest.ini‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ filterwarnings =
2727
ignore:\n\n `numpy.distutils`:DeprecationWarning
2828
# Ignore DeprecationWarning from typing.mypy_plugin
2929
ignore:`numpy.typing.mypy_plugin` is deprecated:DeprecationWarning
30+
# Ignore Runtime Warning from datetime by calculating unitless value and unitful value
31+
ignore:Casting from unitless timedelta to unitful timedelta is ambiguous.:RuntimeWarning
3032
# Ignore DeprecationWarning from struct module
3133
# see https://github.com/numpy/numpy/issues/28926
3234
ignore:Due to \'_pack_\', the

0 commit comments

Comments
 (0)