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

Skip to content

Commit aa19374

Browse files
jklymaktimhoffm
authored andcommitted
Backport PR #12277: FIX: datetime64 now recognized if in a list
1 parent e3909ac commit aa19374

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

lib/matplotlib/dates.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,18 +410,19 @@ def date2num(d):
410410
Gregorian calendar is assumed; this is not universal practice.
411411
For details see the module docstring.
412412
"""
413-
414413
if hasattr(d, "values"):
415414
# this unpacks pandas series or dataframes...
416415
d = d.values
417-
418-
if ((isinstance(d, np.ndarray) and np.issubdtype(d.dtype, np.datetime64))
419-
or isinstance(d, np.datetime64)):
420-
return _dt64_to_ordinalf(d)
421-
if not cbook.iterable(d):
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)
422420
return _to_ordinalf(d)
421+
423422
else:
424423
d = np.asarray(d)
424+
if np.issubdtype(d.dtype, np.datetime64):
425+
return _dt64_to_ordinalf(d)
425426
if not d.size:
426427
return d
427428
return _to_ordinalf_np_vectorized(d)

lib/matplotlib/tests/test_dates.py

Lines changed: 6 additions & 0 deletions
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)