Skip to content

Commit 7b3b00a

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 8ada6ec commit 7b3b00a

15 files changed

Lines changed: 171 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
@@ -2543,6 +2543,16 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj,
25432543
meta->base = NPY_FR_GENERIC;
25442544
meta->num = 1;
25452545
}
2546+
/* If unsafe casting skip this warning (e.g. np.ones_like, np.zeros_like) */
2547+
/* If output is NaT, skip this warning. */
2548+
if(casting != NPY_UNSAFE_CASTING && *out != NPY_DATETIME_NAT && meta->base == NPY_FR_GENERIC) {
2549+
if (DEPRECATE_FUTUREWARNING(
2550+
"Using 'generic' unit for NumPy timedelta is deprecated, "
2551+
"and will raise an error in the future. Please use a "
2552+
"specific units instead.") < 0) {
2553+
return -1;
2554+
}
2555+
}
25462556

25472557
return 0;
25482558
}
@@ -2554,6 +2564,14 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj,
25542564
meta->base = NPY_DATETIME_DEFAULTUNIT;
25552565
meta->num = 1;
25562566
}
2567+
if (meta->base == NPY_FR_GENERIC && casting != NPY_UNSAFE_CASTING) {
2568+
if (DEPRECATE_FUTUREWARNING(
2569+
"Using 'generic' unit for NumPy timedelta is deprecated, "
2570+
"and will raise an error in the future. "
2571+
"Please use a specific units instead.") < 0) {
2572+
return -1;
2573+
}
2574+
}
25572575

25582576
*out = PyLong_AsLongLong(obj);
25592577
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

@@ -426,13 +426,15 @@ def test_coercion_basic(self, dtype, scalar):
426426
ass[()] = scalar
427427
assert_array_equal(ass, cast)
428428

429+
@pytest.mark.filterwarnings("ignore::FutureWarning")
429430
@pytest.mark.parametrize("dtype", [np.int64, np.float32])
430-
@pytest.mark.parametrize("scalar",
431-
[param(np.timedelta64(123, "ns"), id="timedelta64[ns]"),
432-
param(np.timedelta64(12, "generic"), id="timedelta64[generic]")])
433-
def test_coercion_timedelta_convert_to_number(self, dtype, scalar):
431+
@pytest.mark.parametrize("value, unit",
432+
[param(123, "ns", id="timedelta64[ns]"),
433+
param(12, "generic", id="timedelta64[generic]")])
434+
def test_coercion_timedelta_convert_to_number(self, dtype, value, unit):
434435
# Only "ns" and "generic" timedeltas can be converted to numbers
435436
# so these are slightly special.
437+
scalar = np.timedelta64(value, unit)
436438
arr = np.array(scalar, dtype=dtype)
437439
cast = np.array(scalar).astype(dtype)
438440
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
@@ -1247,7 +1247,7 @@ def test_scalar_repr_numbers(dtype, value):
12471247
(np.str_('a'), "'a'", "np.str_('a')"),
12481248
(np.datetime64("2012"),
12491249
"numpy.datetime64('2012')", "np.datetime64('2012')"),
1250-
(np.timedelta64(1), "numpy.timedelta64(1)", "np.timedelta64(1)"),
1250+
(np.timedelta64(1, 's'), "numpy.timedelta64(1,'s')", "np.timedelta64(1,'s')"),
12511251
(np.void((True, 2), dtype="?,<i8"),
12521252
"(True, 2)",
12531253
"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)