2017-11-24 19 views
2

matplotlibのplot_dateで次のエラーが表示されます - 下のjupyterノートブックの例です。matplotlib plot_date AttributeError: 'numpy.datetime64'オブジェクトに 'toordinal'属性がありません

Pythonバージョン3.6.3

パンダのバージョン0.21.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との配列のリスト値:

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

enter image description here

はAttributeError - DataTimeIndexとパンダのDATAFRAME:

:私のpython 3.4.5を使用してエラーがない、matplotlibの1.5.1、パンダ0.19.2時点

編集開始

Pandas v0.21.0 What's Newおよびduplicate answerの回避策のために、以下の行を挿入してください。

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' 
+0

python 2.7.14で同じエラーが発生するので、タグの終了情報をokに追加します.... – ntg

+1

このコードはpandas 0.20で正常に動作します。 pandas 0.21で、彼らはpandasモジュールから 'numpy.datetime64'サポートのために必要なコンバータをインポートし、それをそれぞれのサブモジュールに入れました。したがって、そこからカバレッジをインポートすることができます。方法と根本的な問題へのそれぞれのリンクについては、[duplicate](https://stackoverflow.com/questions/47404653/pandas-0-21-0-timestamp-compatibility-issue-with-matplotlib)を参照してください。 – ImportanceOfBeingErnest

+0

注釈+1!実際、リダイレクションは有効です。メモ:これは答えと実際の失敗の背後にある理由の点で重複していますが、元の質問にはエラーは言及されていません(matplotlib plot_date AttributeError: 'numpy.datetime64'オブジェクトには 'toordinal'属性がありません)。リダイレクトなしで見つけるのはほとんど不可能です。このような理由から私は元の質問を編集する/それを見つける方法を残すだろう。 – ntg

答えて

1

は、将来的に固定することができる何かのように見える...しかし今のところ、私はPythonの日時に私のパンダの日時を変換して使用すること

pydatetime = pd.Timestamp(item['datetime']).to_pydatetime() 
関連する問題