Sina
Sina

Reputation: 2048

Python matplotlib.dates.date2num: converting numpy array to matplotlib datetimes

I am trying to plot a custom chart with datetime axis. My understanding is that matplotlib requires a float format which is days since epoch. So, I want to convert a numpy array to the float epoch as required by matplotlib.

The datetime values are stored in a numpy array called t:

In [235]: t
Out[235]: array(['2008-12-01T00:00:59.000000000-0800',
                 '2008-12-01T00:00:59.000000000-0800',
                 '2008-12-01T00:00:59.000000000-0800',
                 '2008-12-01T00:09:26.000000000-0800',
                 '2008-12-01T00:09:41.000000000-0800'], dtype='datetime64[ns]')

Apparently, matplotlib.dates.date2num only accepts a sequence of python datetimes as input (not numpy datetimes arrays):

import matplotlib.dates as dates
plt_dates = dates.date2num(t)

raises AttributeError: 'numpy.datetime64' object has no attribute 'toordinal'

How should I resolve this issue? I hope to have a solution that works for all types of numpy.datetime like object.

My best workaround (which I am not sure to be correct) is not to use date2num at all. Instead, I try to use the following:

z = np.array([0]).astype(t.dtype)
plt_dates = (t - z)/ np.timedelta64(1,'D')

Even, if this solution is correct, it is nicer to use library functions, instead of manual adhoc workarounds.

Upvotes: 8

Views: 36278

Answers (1)

ntg
ntg

Reputation: 14165

For a quick fix, use:

import matplotlib.dates as dates
plt_dates = dates.date2num(t.to_pydatetime())

or:

import matplotlib.dates as dates
plt_dates = dates.date2num(list(t))

It seems the latest (matplotlib.__version__ '2.1.0') does not like numpy arrays... Edit: In my case, after checking the source code, the problem seems to be that the latest matplotlib.cbook cannot create an iterable from the numpy array and thinks the array is a number.

For similar but a bit more complex problems, check http://stackoverflow.com/questions/13703720/converting-between-datetime-timestamp-and-datetime64, possibly Why do I get "python int too large to convert to C long" errors when I use matplotlib's DateFormatter to format dates on the x axis?, and maybe matplotlib plot_date AttributeError: 'numpy.datetime64' object has no attribute 'toordinal' (if someone answers) Edit: someone answered, his code using to_pydatetime() seems best, also: pandas 0.21.0 Timestamp compatibility issue with matplotlib, though that did not work in my case (because of python 2???)

Upvotes: 12

Related Questions