diff options
author | Tyler Reddy <tyler.je.reddy@gmail.com> | 2018-09-01 19:40:46 -0700 |
---|---|---|
committer | Tyler Reddy <tyler.je.reddy@gmail.com> | 2018-09-01 19:40:46 -0700 |
commit | 4a71482c93c586bbf751f073604fc0e23b106f0d (patch) | |
tree | 81dfae3e8c7edf67fca481cdeeb714619c566faf /numpy/core | |
parent | 4fc43793c73bfc50bedab979b6754843305d108c (diff) | |
download | numpy-4a71482c93c586bbf751f073604fc0e23b106f0d.tar.gz |
BUG: timedelta64 now accepts np ints.
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/src/multiarray/datetime.c | 10 | ||||
-rw-r--r-- | numpy/core/tests/test_datetime.py | 15 |
2 files changed, 25 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/datetime.c b/numpy/core/src/multiarray/datetime.c index 7f837901c..a8550d958 100644 --- a/numpy/core/src/multiarray/datetime.c +++ b/numpy/core/src/multiarray/datetime.c @@ -2845,6 +2845,16 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj, *out = NPY_DATETIME_NAT; return 0; } + else if (PyArray_IsScalar(obj, Integer)) { + /* Use the default unit if none was specified */ + if (meta->base == NPY_FR_ERROR) { + meta->base = NPY_DATETIME_DEFAULTUNIT; + meta->num = 1; + } + + *out = PyLong_AsLongLong(obj); + return 0; + } else { PyErr_SetString(PyExc_ValueError, "Could not convert object to NumPy timedelta"); diff --git a/numpy/core/tests/test_datetime.py b/numpy/core/tests/test_datetime.py index a5e1f73ce..8356615c1 100644 --- a/numpy/core/tests/test_datetime.py +++ b/numpy/core/tests/test_datetime.py @@ -260,6 +260,21 @@ class TestDateTime(object): arr = np.array([dt, dt]).astype('datetime64') assert_equal(arr.dtype, np.dtype('M8[us]')) + @pytest.mark.parametrize("unit", [ + # test all date / time units and use + # "generic" to select generic unit + ("Y"), ("M"), ("W"), ("D"), ("h"), ("m"), + ("s"), ("ms"), ("us"), ("ns"), ("ps"), + ("fs"), ("as"), ("generic") ]) + def test_timedelta_np_int_construction(self, unit): + # regression test for gh-7617 + if unit != "generic": + assert_equal(np.timedelta64(np.int64(123), unit), + np.timedelta64(123, unit)) + else: + assert_equal(np.timedelta64(np.int64(123)), + np.timedelta64(123)) + def test_timedelta_scalar_construction(self): # Construct with different units assert_equal(np.timedelta64(7, 'D'), |