Skip to content

Commit 09c54fa

Browse files
committed
DEP: deprecate the generic unit in numpy.timedelta64 and numpy.datetime64
The ``generic`` unit in `numpy.timedelta64` and `numpy.datetime64` is now deprecated. Using it will raise a ``FutureWarning``. (See numpy#28287)
1 parent 3dec0a5 commit 09c54fa

16 files changed

Lines changed: 173 additions & 61 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Deprecation of the ``generic`` unit in `numpy.timedelta64` and `numpy.datetime64`
2+
---------------------------------------------------------------------------------
3+
4+
The ``generic`` unit in `numpy.timedelta64` and `numpy.datetime64` is now
5+
deprecated. This unit can lead to unexpected behavior in some cases
6+
(see #28287 for details).
7+
8+
As an alternative, please specify an explicit unit such as ``'s'`` (seconds)
9+
or ``'D'`` (days) when constructing `numpy.timedelta64` or `numpy.datetime64`.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,6 +2559,16 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj,
25592559
meta->base = NPY_FR_GENERIC;
25602560
meta->num = 1;
25612561
}
2562+
/* If unsafe casting skip this warning (e.g. np.ones_like, np.zeros_like) */
2563+
/* If output is NaT, skip this warning. */
2564+
if(casting != NPY_UNSAFE_CASTING && *out != NPY_DATETIME_NAT && meta->base == NPY_FR_GENERIC) {
2565+
if (DEPRECATE_FUTUREWARNING(
2566+
"Using 'generic' unit for NumPy timedelta is deprecated, "
2567+
"and will raise an error in the future. Please use a "
2568+
"specific units instead.") < 0) {
2569+
return -1;
2570+
}
2571+
}
25622572

25632573
return 0;
25642574
}
@@ -2570,6 +2580,14 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj,
25702580
meta->base = NPY_DATETIME_DEFAULTUNIT;
25712581
meta->num = 1;
25722582
}
2583+
if (meta->base == NPY_FR_GENERIC && casting != NPY_UNSAFE_CASTING) {
2584+
if (DEPRECATE_FUTUREWARNING(
2585+
"Using 'generic' unit for NumPy timedelta is deprecated, "
2586+
"and will raise an error in the future. "
2587+
"Please use a specific units instead.") < 0) {
2588+
return -1;
2589+
}
2590+
}
25732591

25742592
*out = PyLong_AsLongLong(obj);
25752593
if (error_converting(*out)) {

‎numpy/_core/tests/test_array_coercion.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def scalar_instances(times=True, extended_precision=True, user_dtype=True):
121121

122122
if times:
123123
# Datetimes and timedelta
124-
yield param(np.timedelta64(2), id="timedelta64[generic]")
124+
yield param(np.timedelta64(2, "ns"), id="timedelta64[ns]")
125125
yield param(np.timedelta64(23, "s"), id="timedelta64[s]")
126126
yield param(np.timedelta64("NaT", "s"), id="timedelta64[s](NaT)")
127127

@@ -421,13 +421,15 @@ def test_coercion_basic(self, dtype, scalar):
421421
ass[()] = scalar
422422
assert_array_equal(ass, cast)
423423

424+
@pytest.mark.filterwarnings("ignore::FutureWarning")
424425
@pytest.mark.parametrize("dtype", [np.int64, np.float32])
425-
@pytest.mark.parametrize("scalar",
426-
[param(np.timedelta64(123, "ns"), id="timedelta64[ns]"),
427-
param(np.timedelta64(12, "generic"), id="timedelta64[generic]")])
428-
def test_coercion_timedelta_convert_to_number(self, dtype, scalar):
426+
@pytest.mark.parametrize("value, unit",
427+
[param(123, "ns", id="timedelta64[ns]"),
428+
param(12, "generic", id="timedelta64[generic]")])
429+
def test_coercion_timedelta_convert_to_number(self, dtype, value, unit):
429430
# Only "ns" and "generic" timedeltas can be converted to numbers
430431
# so these are slightly special.
432+
scalar = np.timedelta64(value, unit)
431433
arr = np.array(scalar, dtype=dtype)
432434
cast = np.array(scalar).astype(dtype)
433435
ass = np.ones((), dtype=dtype)

‎numpy/_core/tests/test_arrayprint.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ def test_scalar_repr_numbers(dtype, value):
12431243
(np.str_('a'), "'a'", "np.str_('a')"),
12441244
(np.datetime64("2012"),
12451245
"numpy.datetime64('2012')", "np.datetime64('2012')"),
1246-
(np.timedelta64(1), "numpy.timedelta64(1)", "np.timedelta64(1)"),
1246+
(np.timedelta64(1, 's'), "numpy.timedelta64(1,'s')", "np.timedelta64(1,'s')"),
12471247
(np.void((True, 2), dtype="?,<i8"),
12481248
"(True, 2)",
12491249
"np.void((True, 2), dtype=[('f0', '?'), ('f1', '<i8')])"),

‎numpy/_core/tests/test_cython.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def install_temp(tmpdir_factory):
8383
sys.path.append(str(build_dir))
8484

8585

86+
@pytest.mark.filterwarnings("ignore::FutureWarning")
8687
def test_is_timedelta64_object(install_temp):
8788
import checks
8889

@@ -96,6 +97,7 @@ def test_is_timedelta64_object(install_temp):
9697
assert not checks.is_td64(np.datetime64("now", "s"))
9798

9899

100+
@pytest.mark.filterwarnings("ignore::FutureWarning")
99101
def test_is_datetime64_object(install_temp):
100102
import checks
101103

0 commit comments

Comments
 (0)