python
前提・実現したいこと
ローカル保存してある株式投資メモからのcsvをグラフ描画したい
発生している問題・エラーメッセージ
AttributeError Traceback (most recent call last)
<ipython-input-51-675d9ee83f23> in <module>
30 plt.show()
31
---> 32 DataRead()
<ipython-input-51-675d9ee83f23> in DataRead()
10
11 df_ = df.copy()
---> 12 df_.index = mdates.date2num(df_.index)
13 data = df_.reset_index().values
14
~/anaconda3/lib/python3.7/site-packages/matplotlib/dates.py in date2num(d)
424 if not d.size:
425 return d
--> 426 return _to_ordinalf_np_vectorized(d)
427
428
~/anaconda3/lib/python3.7/site-packages/numpy/lib/function_base.py in call(self, *args, **kwargs)
2089 vargs.extend([kwargs[_n] for _n in names])
2090
-> 2091 return self._vectorize_call(func=func, args=vargs)
2092
2093 def _get_ufunc_and_otypes(self, func, args):
~/anaconda3/lib/python3.7/site-packages/numpy/lib/function_base.py in _vectorize_call(self, func, args)
2159 res = func()
2160 else:
-> 2161 ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
2162
2163 # Convert args to object arrays first
~/anaconda3/lib/python3.7/site-packages/numpy/lib/function_base.py in _get_ufunc_and_otypes(self, func, args)
2119
2120 inputs = [arg.flat[0] for arg in args]
-> 2121 outputs = func(*inputs)
2122
2123 # Performance note: profiling indicates that -- for simple
~/anaconda3/lib/python3.7/site-packages/matplotlib/dates.py in _to_ordinalf(dt)
224 tzi = UTC
225
--> 226 base = float(dt.toordinal())
227
228 # If it's sufficiently datetime-like, it will have a date()
method
AttributeError: 'str' object has no attribute 'toordinal'
該当のソースコード
python
1import pandas as pd 2import matplotlib.pyplot as plt 3import matplotlib.dates as mdates 4import mpl_finance as mpf 5import codecs as cd 6 7def DataRead(): 8 with cd.open("/Users/super/9399_2019.csv", "r", "Shift-JIS", "ignore") as csv_file: 9 df = pd.read_csv(csv_file, quotechar='"', header=1, index_col=0) 10 11 df_ = df.copy() 12 df_.index = mdates.date2num(df_.index) 13 data = df_.reset_index().values 14 15 fig = plt.figure(figsize=(12, 4)) 16 ax = fig.add_subplot(1, 1, 1) 17 18 mpf.candlestick_ohlc(ax, data, width=2, alpha=1.0, colorup='r', colordown='b') 19 ax.plot(df.index, df['close'].rolling(5).mean(),color='g',label="Moving Ave(5)") 20 ax.plot(df.index, df['close'].rolling(25).mean(),color='m',label="Moving Ave(25)") 21 ax.plot(df.index, df['close'].rolling(50).mean(),color='r',label="Moving Ave(50)") 22 plt.scatter(x, y, s=100, marker="v",color='k') 23 24 ax.grid() 25 26 locator = mdates.AutoDateLocator() 27 ax.xaxis.set_major_locator(locator) 28 ax.xaxis.set_major_formatter(mdates.AutoDateFormatter(locator)) 29 plt.legend() 30 plt.show() 31 32DataRead() 33
試したこと
大文字小文字やスペルミスがよくある間違いだそうなのでそこを中心に確認しております
補足情報(FW/ツールのバージョンなど)
[https://qiita.com/Nabetalion/items/73b9bd3adec62584ee3a]
上記サイトを見本として作業しています