Skip to content

Commit

Permalink
BUG: fix crash when using arange on datetime without dtype set
Browse files Browse the repository at this point in the history
  • Loading branch information
juliantaylor committed Oct 23, 2014
1 parent a4e68f4 commit 0332a2d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions numpy/core/src/multiarray/datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -3047,7 +3047,7 @@ cast_timedelta_to_timedelta(PyArray_DatetimeMetaData *src_meta,
* Returns true if the object is something that is best considered
* a Datetime, false otherwise.
*/
static npy_bool
static NPY_GCC_NONNULL(1) npy_bool
is_any_numpy_datetime(PyObject *obj)
{
return (PyArray_IsScalar(obj, Datetime) ||
Expand Down Expand Up @@ -3296,7 +3296,8 @@ datetime_arange(PyObject *start, PyObject *stop, PyObject *step,
}
}
else {
if (is_any_numpy_datetime(start) || is_any_numpy_datetime(stop)) {
if ((start && is_any_numpy_datetime(start)) ||
is_any_numpy_datetime(stop)) {
type_nums[0] = NPY_DATETIME;
}
else {
Expand Down
10 changes: 10 additions & 0 deletions numpy/core/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,11 @@ def test_datetime_arange(self):
np.datetime64('2012-02-03T14Z', 's'),
np.timedelta64(5, 'Y'))

def test_datetime_arange_no_dtype(self):
d = np.array('2010-01-04', dtype="M8[D]")
assert_equal(np.arange(d, d + 1), d)
assert_raises(ValueError, np.arange, d)

def test_timedelta_arange(self):
a = np.arange(3, 10, dtype='m8')
assert_equal(a.dtype, np.dtype('m8'))
Expand All @@ -1430,6 +1435,11 @@ def test_timedelta_arange(self):
assert_raises(TypeError, np.arange, np.timedelta64(0, 'Y'),
np.timedelta64(5, 'D'))

def test_timedelta_arange_no_dtype(self):
d = np.array(5, dtype="m8[D]")
assert_equal(np.arange(d, d + 1), d)
assert_raises(ValueError, np.arange, d)

def test_datetime_maximum_reduce(self):
a = np.array(['2010-01-02', '1999-03-14', '1833-03'], dtype='M8[D]')
assert_equal(np.maximum.reduce(a).dtype, np.dtype('M8[D]'))
Expand Down

0 comments on commit 0332a2d

Please sign in to comment.