bir*_*one 7 python matplotlib pandas
一些Matplotlib方法需要几天"浮动日格式".datestr2num是一个转换器函数,但它与相关的pandas对象有关:
In [3]: type(df.index)
Out[3]: pandas.tseries.index.DatetimeIndex
In [4]: type(df.index[0])
Out[4]: pandas.tslib.Timestamp
In [5]: mpl.dates.date2num(df.index)
Out [5]: ...
AttributeError: 'numpy.datetime64' object has no attribute 'toordinal'
这提供了'浮动日格式'的可用时间列表:
dates = [mpl.dates.date2num(t) for t in df.index]
但有更好的方法吗?
jor*_*ris 10
你可以使用to_pydatetimeDatetimeIndex 的方法(这会将它转换为datetime.datetime's 的数组,mpl.dates.date2num并将知道如何处理它们):
mpl.dates.date2num(df.index.to_pydatetime())
本date2num机不处理pandas DatetimeIndex 的原因是因为matplotlib还不支持numpy datetime64 dtype(这是数据存储在DatetimeIndex中的方式).