Skip to content

Commit

Permalink
Merge pull request numpy#11097 from shoyer/timedelta-units
Browse files Browse the repository at this point in the history
BUG: fix datetime.timedelta->timedelta64 unit detection logic
  • Loading branch information
eric-wieser authored May 26, 2018
2 parents c1fc882 + 9ceb49b commit 69503ff
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
5 changes: 4 additions & 1 deletion numpy/core/src/multiarray/datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2808,9 +2808,12 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj,
us_meta.base = NPY_FR_m;
}
else if (td % (24*60*60*1000000LL) != 0) {
us_meta.base = NPY_FR_D;
us_meta.base = NPY_FR_h;
}
else if (td % (7*24*60*60*1000000LL) != 0) {
us_meta.base = NPY_FR_D;
}
else {
us_meta.base = NPY_FR_W;
}
us_meta.num = 1;
Expand Down
20 changes: 19 additions & 1 deletion numpy/core/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_datetime_casting_rules(self):
assert_(not np.can_cast('M8[h]', 'M8', casting='safe'))

def test_compare_generic_nat(self):
# regression tests for GH6452
# regression tests for gh-6452
assert_equal(np.datetime64('NaT'),
np.datetime64('2000') + np.timedelta64('NaT'))
# nb. we may want to make NaT != NaT true in the future
Expand Down Expand Up @@ -331,6 +331,24 @@ def test_timedelta_scalar_construction(self):
a = np.timedelta64(1, 'Y')
assert_raises(TypeError, np.timedelta64, a, 'D')
assert_raises(TypeError, np.timedelta64, a, 'm')
a = datetime.timedelta(seconds=3)
assert_raises(TypeError, np.timedelta64, a, 'M')
assert_raises(TypeError, np.timedelta64, a, 'Y')
a = datetime.timedelta(weeks=3)
assert_raises(TypeError, np.timedelta64, a, 'M')
assert_raises(TypeError, np.timedelta64, a, 'Y')
a = datetime.timedelta()
assert_raises(TypeError, np.timedelta64, a, 'M')
assert_raises(TypeError, np.timedelta64, a, 'Y')

def test_timedelta_object_array_conversion(self):
# Regression test for gh-11096
inputs = [datetime.timedelta(28),
datetime.timedelta(30),
datetime.timedelta(31)]
expected = np.array([28, 30, 31], dtype='timedelta64[D]')
actual = np.array(inputs, dtype='timedelta64[D]')
assert_equal(expected, actual)

def test_timedelta_scalar_construction_units(self):
# String construction detecting units
Expand Down

0 comments on commit 69503ff

Please sign in to comment.