Skip to content

Commit 1c17868

Browse files
jklymaktimhoffm
authored andcommitted
FIX: datetime64 now recognized if in a list (matplotlib#12277)
* FIX: datetime64 now recognized if in a list * TST: test list datetime64 converts
1 parent ea031c2 commit 1c17868

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

lib/matplotlib/dates.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -405,18 +405,19 @@ def date2num(d):
405405
Gregorian calendar is assumed; this is not universal practice.
406406
For details see the module docstring.
407407
"""
408-
409408
if hasattr(d, "values"):
410409
# this unpacks pandas series or dataframes...
411410
d = d.values
412-
413-
if ((isinstance(d, np.ndarray) and np.issubdtype(d.dtype, np.datetime64))
414-
or isinstance(d, np.datetime64)):
415-
return _dt64_to_ordinalf(d)
416411
if not np.iterable(d):
412+
if (isinstance(d, np.datetime64) or (isinstance(d, np.ndarray) and
413+
np.issubdtype(d.dtype, np.datetime64))):
414+
return _dt64_to_ordinalf(d)
417415
return _to_ordinalf(d)
416+
418417
else:
419418
d = np.asarray(d)
419+
if np.issubdtype(d.dtype, np.datetime64):
420+
return _dt64_to_ordinalf(d)
420421
if not d.size:
421422
return d
422423
return _to_ordinalf_np_vectorized(d)

lib/matplotlib/tests/test_dates.py

+6
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,9 @@ def test_tz_utc():
641641
def test_num2timedelta(x, tdelta):
642642
dt = mdates.num2timedelta(x)
643643
assert dt == tdelta
644+
645+
646+
def test_datetime64_in_list():
647+
dt = [np.datetime64('2000-01-01'), np.datetime64('2001-01-01')]
648+
dn = mdates.date2num(dt)
649+
assert np.array_equal(dn, [730120., 730486.])

0 commit comments

Comments
 (0)