Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c569ca7

Browse files
committed
FIX: translate timedelta64 in _dt64_to_ordinalf
1 parent 3a5683b commit c569ca7

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

lib/matplotlib/dates.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ def _dt64_to_ordinalf(d):
255255
because we do times compared to ``0001-01-01T00:00:00`` (plus one day).
256256
"""
257257

258+
if isinstance(d, np.timedelta64):
259+
return d / np.timedelta64(1, 'D')
260+
258261
# the "extra" ensures that we at least allow the dynamic range out to
259262
# seconds. That should get out to +/-2e11 years.
260263
# NOTE: First cast truncates; second cast back is for NumPy 1.10.
@@ -275,6 +278,13 @@ def _dt64_to_ordinalf(d):
275278
return dt
276279

277280

281+
def _dt64_to_ordinalf_iterable(d):
282+
dnew = np.zeros(len(d))
283+
for nn, dd in enumerate(d):
284+
dnew[nn] = _dt64_to_ordinalf(dd)
285+
return(dnew)
286+
287+
278288
def _from_ordinalf(x, tz=None):
279289
"""
280290
Convert Gregorian float of the date, preserving hours, minutes,
@@ -413,19 +423,32 @@ def date2num(d):
413423
if hasattr(d, "values"):
414424
# this unpacks pandas series or dataframes...
415425
d = d.values
416-
if not np.iterable(d):
417-
if (isinstance(d, np.datetime64) or (isinstance(d, np.ndarray) and
418-
np.issubdtype(d.dtype, np.datetime64))):
419-
return _dt64_to_ordinalf(d)
420-
return _to_ordinalf(d)
421426

422-
else:
423-
d = np.asarray(d)
424-
if np.issubdtype(d.dtype, np.datetime64):
427+
if hasattr(d, 'size') and not d.size:
428+
# this is an empty
429+
return d
430+
431+
if not np.iterable(d) and not isinstance(d, np.ndarray):
432+
# single value logic...
433+
if (isinstance(d, np.datetime64) or isinstance(d, np.timedelta64)):
425434
return _dt64_to_ordinalf(d)
426-
if not d.size:
427-
return d
435+
else:
436+
return _to_ordinalf(d)
437+
438+
elif (isinstance(d, np.ndarray) and
439+
(np.issubdtype(d.dtype, np.datetime64) or
440+
np.issubdtype(d.dtype, np.timedelta64))):
441+
# array with all one type of datetime64 object.
442+
return _dt64_to_ordinalf(d)
443+
444+
elif len(d):
445+
# this is a list or tuple...
446+
if (isinstance(d[0], np.datetime64) or
447+
isinstance(d[0], np.timedelta64)):
448+
return _dt64_to_ordinalf_iterable(d)
428449
return _to_ordinalf_np_vectorized(d)
450+
else:
451+
return []
429452

430453

431454
def julian2num(j):

lib/matplotlib/tests/test_dates.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,3 +680,21 @@ def test_datetime64_in_list():
680680
dt = [np.datetime64('2000-01-01'), np.datetime64('2001-01-01')]
681681
dn = mdates.date2num(dt)
682682
assert np.array_equal(dn, [730120., 730486.])
683+
684+
685+
def test_timedelta():
686+
"""
687+
test that timedelta objects are properly translated into days
688+
"""
689+
dt = [datetime.datetime(2000, 1, 1, 0, 0, 0),
690+
datetime.timedelta(days=1, hours=2)]
691+
assert mdates.date2num(dt[1]) == 1 + 2 / 24
692+
# check that mixed lists work....
693+
assert mdates.date2num(dt)[0] == 730120.0
694+
assert mdates.date2num(dt)[1] == 1 + 2 / 24
695+
dt = (np.datetime64('2000-01-01'),
696+
np.timedelta64(26, 'h'))
697+
assert mdates.date2num(dt[1]) == 1 + 2 / 24
698+
# check that mixed lists work....
699+
assert mdates.date2num(dt)[0] == 730120.0
700+
assert mdates.date2num(dt)[1] == 1 + 2 / 24

0 commit comments

Comments
 (0)