matplotlib plot_date AttributeError:'numpy.datetime64'对象没有属性'toordinal' [英] matplotlib plot_date AttributeError: 'numpy.datetime64' object has no attribute 'toordinal'

查看:98
本文介绍了matplotlib plot_date AttributeError:'numpy.datetime64'对象没有属性'toordinal'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

matplotlib 中的 plot_date 出现以下错误 - 下面的 jupyter notebook 示例.

I'm getting the following error with plot_date in matplotlib - example in jupyter notebook below.

python版本3.6.3

python version 3.6.3

熊猫版本 0.21.0

pandas version 0.21.0

matplotlib 2.1.0

matplotlib 2.1.0

import pandas as pd
%pylab inline

datelist = pd.date_range(pd.datetime.today(), periods=100).tolist()
vals = np.random.rand(100,1)

没有错误 - pandas._libs.tslib.Timestamp 列表和值数组:

No error - list of pandas._libs.tslib.Timestamp and array of values:

plt.plot_date(datelist, vals, xdate=True);

AttributeError -具有DataTimeIndex的熊猫DataFrame:

AttributeError - pandas DataFrame with DataTimeIndex:

注意:使用python 3.4.5、matplotlib 1.5.1、pandas 0.19.2时没有报错

Note: there is no error when I use python 3.4.5, matplotlib 1.5.1, pandas 0.19.2

编辑开始

按照Pandas v0.21.0 新增功能重复答案.

from pandas.tseries import converter
converter.register() 

编辑结束

df = pd.DataFrame(data=vals, index=datelist)

plt.plot_date(df.index, df[0], xdate=True);

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-22-c438634aa06b> in <module>()
----> 1 plt.plot_date(df.index, df[0], xdate=True);

~/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py in plot_date(x, y, fmt, tz, xdate, ydate, hold, data, **kwargs)
   3261     try:
   3262         ret = ax.plot_date(x, y, fmt=fmt, tz=tz, xdate=xdate, ydate=ydate,
-> 3263                            data=data, **kwargs)
   3264     finally:
   3265         ax._hold = washold

~/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1708                     warnings.warn(msg % (label_namer, func.__name__),
   1709                                   RuntimeWarning, stacklevel=2)
-> 1710             return func(ax, *args, **kwargs)
   1711         pre_doc = inner.__doc__
   1712         if pre_doc is None:

~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in plot_date(self, x, y, fmt, tz, xdate, ydate, **kwargs)
   1515             self.yaxis_date(tz)
   1516 
-> 1517         ret = self.plot(x, y, fmt, **kwargs)
   1518 
   1519         self.autoscale_view()

~/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1708                     warnings.warn(msg % (label_namer, func.__name__),
   1709                                   RuntimeWarning, stacklevel=2)
-> 1710             return func(ax, *args, **kwargs)
   1711         pre_doc = inner.__doc__
   1712         if pre_doc is None:

~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in plot(self, *args, **kwargs)
   1436 
   1437         for line in self._get_lines(*args, **kwargs):
-> 1438             self.add_line(line)
   1439             lines.append(line)
   1440 

~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py in add_line(self, line)
   1757             line.set_clip_path(self.patch)
   1758 
-> 1759         self._update_line_limits(line)
   1760         if not line.get_label():
   1761             line.set_label('_line%d' % len(self.lines))

~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py in _update_line_limits(self, line)
   1779         Figures out the data limit of the given line, updating self.dataLim.
   1780         """
-> 1781         path = line.get_path()
   1782         if path.vertices.size == 0:
   1783             return

~/anaconda3/lib/python3.6/site-packages/matplotlib/lines.py in get_path(self)
    949         """
    950         if self._invalidy or self._invalidx:
--> 951             self.recache()
    952         return self._path
    953 

~/anaconda3/lib/python3.6/site-packages/matplotlib/lines.py in recache(self, always)
    649     def recache(self, always=False):
    650         if always or self._invalidx:
--> 651             xconv = self.convert_xunits(self._xorig)
    652             x = _to_unmasked_float_array(xconv).ravel()
    653         else:

~/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py in convert_xunits(self, x)
    189         if ax is None or ax.xaxis is None:
    190             return x
--> 191         return ax.xaxis.convert_units(x)
    192 
    193     def convert_yunits(self, y):

~/anaconda3/lib/python3.6/site-packages/matplotlib/axis.py in convert_units(self, x)
   1489             return x
   1490 
-> 1491         ret = self.converter.convert(x, self.units, self)
   1492         return ret
   1493 

~/anaconda3/lib/python3.6/site-packages/matplotlib/dates.py in convert(value, unit, axis)
   1601         if units.ConversionInterface.is_numlike(value):
   1602             return value
-> 1603         return date2num(value)
   1604 
   1605     @staticmethod

~/anaconda3/lib/python3.6/site-packages/matplotlib/dates.py in date2num(d)
    371         if not d.size:
    372             return d
--> 373         return _to_ordinalf_np_vectorized(d)
    374 
    375 

~/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in __call__(self, *args, **kwargs)
   2732             vargs.extend([kwargs[_n] for _n in names])
   2733 
-> 2734         return self._vectorize_call(func=func, args=vargs)
   2735 
   2736     def _get_ufunc_and_otypes(self, func, args):

~/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in _vectorize_call(self, func, args)
   2802             res = func()
   2803         else:
-> 2804             ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
   2805 
   2806             # Convert args to object arrays first

~/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in _get_ufunc_and_otypes(self, func, args)
   2762 
   2763             inputs = [arg.flat[0] for arg in args]
-> 2764             outputs = func(*inputs)
   2765 
   2766             # Performance note: profiling indicates that -- for simple

~/anaconda3/lib/python3.6/site-packages/matplotlib/dates.py in _to_ordinalf(dt)
    220         tzi = UTC
    221 
--> 222     base = float(dt.toordinal())
    223 
    224     # If it's sufficiently datetime-like, it will have a `date()` method

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

推荐答案

看起来可能会在未来修复...但现在,我将我的 Pandas datetime 转换为 python datetime 并使用了

Looks like something that may be fixed in the future...but for now, I converted my pandas datetime into a python datetime and used that

pydatetime = pd.Timestamp(item['datetime']).to_pydatetime()

这篇关于matplotlib plot_date AttributeError:'numpy.datetime64'对象没有属性'toordinal'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆