Skip to content

Commit 50edd9f

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 6059403 commit 50edd9f

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;
@@ -3903,9 +3908,6 @@ time_to_time_resolve_descriptors(
39033908
return NPY_EQUIV_CASTING;
39043909
}
39053910
else if (meta1->base == NPY_FR_GENERIC) {
3906-
if (byteorder_may_allow_view) {
3907-
*view_offset = 0;
3908-
}
39093911
return NPY_SAFE_CASTING ;
39103912
}
39113913
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
@@ -414,7 +414,7 @@ def test_numeric_to_times(self, from_Dt):
414414
[("M8[ns]", None, Casting.no, 0, 1, 1),
415415
(str(np.dtype("M8[ns]").newbyteorder()), None,
416416
Casting.equiv, None, 1, 1),
417-
("M8", "M8[ms]", Casting.safe, 0, 1, 1),
417+
("M8", "M8[ms]", Casting.safe, None, 1, 1),
418418
# should be invalid cast:
419419
("M8[ms]", "M8", Casting.unsafe, None, 1, 1),
420420
("M8[5ms]", "M8[5ms]", Casting.no, 0, 1, 1),
@@ -427,7 +427,7 @@ def test_numeric_to_times(self, from_Dt):
427427
("m8[ns]", None, Casting.no, 0, 1, 1),
428428
(str(np.dtype("m8[ns]").newbyteorder()), None,
429429
Casting.equiv, None, 1, 1),
430-
("m8", "m8[ms]", Casting.safe, 0, 1, 1),
430+
("m8", "m8[ms]", Casting.safe, None, 1, 1),
431431
# should be invalid cast:
432432
("m8[ms]", "m8", Casting.unsafe, None, 1, 1),
433433
("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
@@ -24,6 +24,8 @@ filterwarnings =
2424
ignore:Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.:UserWarning
2525
# Ignore DeprecationWarning from typing.mypy_plugin
2626
ignore:`numpy.typing.mypy_plugin` is deprecated:DeprecationWarning
27+
# Ignore Runtime Warning from datetime by calculating unitless value and unitful value
28+
ignore:Casting from unitless timedelta to unitful timedelta is ambiguous.:RuntimeWarning
2729
# Ignore DeprecationWarning from struct module
2830
# see https://github.com/numpy/numpy/issues/28926
2931
ignore:Due to \'_pack_\', the

0 commit comments

Comments
 (0)