Skip to content

Commit bec6488

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

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

lib/matplotlib/dates.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ 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) or
259+
(isinstance(d, np.ndarray) and
260+
np.issubdtype(d.dtype, np.timedelta64))):
261+
return d / np.timedelta64(1, 'D')
262+
258263
# the "extra" ensures that we at least allow the dynamic range out to
259264
# seconds. That should get out to +/-2e11 years.
260265
# NOTE: First cast truncates; second cast back is for NumPy 1.10.
@@ -275,6 +280,13 @@ def _dt64_to_ordinalf(d):
275280
return dt
276281

277282

283+
def _dt64_to_ordinalf_iterable(d):
284+
dnew = np.zeros(len(d))
285+
for nn, dd in enumerate(d):
286+
dnew[nn] = _dt64_to_ordinalf(dd)
287+
return(dnew)
288+
289+
278290
def _from_ordinalf(x, tz=None):
279291
"""
280292
Convert Gregorian float of the date, preserving hours, minutes,
@@ -413,19 +425,31 @@ def date2num(d):
413425
if hasattr(d, "values"):
414426
# this unpacks pandas series or dataframes...
415427
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)
421428

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

430454

431455
def julian2num(j):

lib/matplotlib/tests/test_dates.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,3 +680,35 @@ 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
701+
702+
dt = [datetime.timedelta(days=1, hours=1),
703+
datetime.timedelta(days=1, hours=2)]
704+
assert mdates.date2num(dt)[0] == 1 + 1 / 24
705+
assert mdates.date2num(dt)[1] == 1 + 2 / 24
706+
707+
dt = (np.timedelta64(25, 'h'),
708+
np.timedelta64(26, 'h'))
709+
assert mdates.date2num(dt)[0] == 1 + 1 / 24
710+
assert mdates.date2num(dt)[1] == 1 + 2 / 24
711+
712+
dt = np.array([25, 26], dtype='timedelta64[h]')
713+
assert mdates.date2num(dt)[0] == 1 + 1 / 24
714+
assert mdates.date2num(dt)[1] == 1 + 2 / 24

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy