Python 使用pandas绘制简单时间序列
我试图画一个简单的时间序列图,看看收盘价是如何一天比一天变化的。这是我的代码:Python 使用pandas绘制简单时间序列,python,pandas,matplotlib,Python,Pandas,Matplotlib,我试图画一个简单的时间序列图,看看收盘价是如何一天比一天变化的。这是我的代码: from yahoo_finance import Share from datetime import datetime import pandas as pd import matplotlib.pyplot as plt yahoo = Share('YHOO') stock = yahoo start_date = '2015-12-01' end_date = '2015-12-10' histor
from yahoo_finance import Share
from datetime import datetime
import pandas as pd
import matplotlib.pyplot as plt
yahoo = Share('YHOO')
stock = yahoo
start_date = '2015-12-01'
end_date = '2015-12-10'
historical_table = pd.DataFrame(stock.get_historical(start_date, end_date))
historical_table = historical_table[['Date','Symbol','Close','High','Low','Open']]
def convert_dates(date):
date = datetime.strptime(date, "%Y-%m-%d")
return date
historical_table['Date'].apply(convert_dates)
print historical_table['Date']
x = historical_table['Date']
y = historical_table['Close']
plt.plot(x,y)
plt.show()
这是我收到的错误消息:
Traceback (most recent call last):
File "finance.py", line 45, in <module>
plt.plot(x,y)
File "/Users/paulzovighian/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3099, in plot
ret = ax.plot(*args, **kwargs)
File "/Users/paulzovighian/anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 1373, in plot
for line in self._get_lines(*args, **kwargs):
File "/Users/paulzovighian/anaconda/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 304, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "/Users/paulzovighian/anaconda/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 263, in _plot_args
linestyle, marker, color = _process_plot_format(tup[-1])
File "/Users/paulzovighian/anaconda/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 85, in _process_plot_format
if fmt.find('--') >= 0:
File "/Users/paulzovighian/anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 2246, in __getattr__
(type(self).__name__, name))
AttributeError: 'Series' object has no attribute 'find'
回溯(最近一次呼叫最后一次):
文件“finance.py”,第45行,在
平面图(x,y)
文件“/Users/paulzovighian/anaconda/lib/python2.7/site packages/matplotlib/pyplot.py”,第3099行,在plot中
ret=最大绘图(*args,**kwargs)
文件“/Users/paulzovighian/anaconda/lib/python2.7/site packages/matplotlib/axes/_axes.py”,第1373行,在绘图中
对于自身中的行。获取行(*args,**kwargs):
文件“/Users/paulzovighian/anaconda/lib/python2.7/site packages/matplotlib/axes/_base.py”,第304行,在下一个参数中
对于seg in self.\u plot\u args(剩余,kwargs):
文件“/Users/paulzovighian/anaconda/lib/python2.7/site packages/matplotlib/axes/_base.py”,第263行,在绘图参数中
线型、标记、颜色=\处理\打印\格式(tup[-1])
文件“/Users/paulzovighian/anaconda/lib/python2.7/site packages/matplotlib/axes/_base.py”,第85行,采用“过程图”格式
如果fmt.find('--')>=0:
文件“/Users/paulzovighian/anaconda/lib/python2.7/site packages/pandas/core/generic.py”,第2246行,位于__
(键入(自身)。\名称\名称)
AttributeError:“Series”对象没有属性“find”
在线查看时,我倾向于看到“硬编码”变量的示例,但我不知道如何将其应用于dataframe列-我发现我应该使用strTime来确定日期列的格式,但我不知道这是否有任何影响(如果我注释掉convert_dates apply方法,我会得到相同的错误)
提前感谢您在此提供的帮助,并欢迎您提出任何简化此方法的建议。似乎是格式问题,请尝试以下方法:
# %matplotlib inline # Use this if you are using Jupyter notebook and want plots inline
from yahoo_finance import Share
from datetime import datetime
import pandas as pd
import matplotlib.pyplot as plt
yahoo = Share('YHOO')
stock = yahoo
start_date = '2015-12-01'
end_date = '2015-12-10'
historical_table = pd.DataFrame(stock.get_historical(start_date, end_date))
historical_table = historical_table[['Date','Symbol','Close','High','Low','Open']]
historical_table['Date'] = pd.to_datetime(historical_table['Date']) #date to datetime format
historical_table['Close'] = [float(x) for x in historical_table['Close']] #close price to floats
x = historical_table['Date']
y = historical_table['Close']
plt.plot(x,y)
plt.show()
非常感谢。算了吧,熊猫当然有一个内置的方法来转换为datetime哈哈