Skip to content

FIX: datetime64 now recognized if in a list #12277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,18 +405,19 @@ def date2num(d):
Gregorian calendar is assumed; this is not universal practice.
For details see the module docstring.
"""

if hasattr(d, "values"):
# this unpacks pandas series or dataframes...
d = d.values

if ((isinstance(d, np.ndarray) and np.issubdtype(d.dtype, np.datetime64))
or isinstance(d, np.datetime64)):
return _dt64_to_ordinalf(d)
if not np.iterable(d):
if (isinstance(d, np.datetime64) or (isinstance(d, np.ndarray) and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that we will ever hit the second half of this conditional as

In [5]: np.iterable(np.arange(5))
Out[5]: True

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t0 = datetime.datetime(2017, 1, 1, 0, 1, 1)
tnp = np.array(t0, dtype='datetime64[us]')
print(np.iterable(tnp))

returns False (believe it or not)...

np.issubdtype(d.dtype, np.datetime64))):
return _dt64_to_ordinalf(d)
return _to_ordinalf(d)

else:
d = np.asarray(d)
if np.issubdtype(d.dtype, np.datetime64):
return _dt64_to_ordinalf(d)
if not d.size:
return d
return _to_ordinalf_np_vectorized(d)
Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,3 +641,9 @@ def test_tz_utc():
def test_num2timedelta(x, tdelta):
dt = mdates.num2timedelta(x)
assert dt == tdelta


def test_datetime64_in_list():
dt = [np.datetime64('2000-01-01'), np.datetime64('2001-01-01')]
dn = mdates.date2num(dt)
assert np.array_equal(dn, [730120., 730486.])