Financial Plots in Python

时间:2016-02-03 03:03:30

标签: python

I am trying to graph candlestick stock charts in python. The code I have works fine except the last line generates an error message and I don't know how to fix it. I appreciate any help.

import plotly
plotly.__version__


import plotly.plotly as py
from plotly.tools import FigureFactory as FF
from datetime import datetime

import pandas.io.data as web

df = web.DataReader("aapl", 'yahoo', datetime(2007, 10, 1), datetime(2009, 4, 1))
fig = FF.create_candlestick(df.Open, df.High, df.Low, df.Close, dates=df.index)
py.iplot(fig, filename='finance_aapl_candlestick', validate=False)

>>> py.iplot(fig, filename='finance_aapl_candlestick', validate=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\plotly\plotly\plotly.py", line 151, in iplot
    url = plot(figure_or_data, **plot_options)
  File "C:\Python27\lib\site-packages\plotly\plotly\plotly.py", line 241, in plot
    res = _send_to_plotly(figure, **plot_options)
  File "C:\Python27\lib\site-packages\plotly\plotly\plotly.py", line 1374, in _send_to_plotly
    cls=utils.PlotlyJSONEncoder)
  File "C:\Python27\lib\json\__init__.py", line 250, in dumps
    sort_keys=sort_keys, **kw).encode(obj)
  File "C:\Python27\lib\site-packages\plotly\utils.py", line 145, in encode
    encoded_o = super(PlotlyJSONEncoder, self).encode(o)
  File "C:\Python27\lib\json\encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Python27\lib\json\encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "C:\Python27\lib\site-packages\plotly\utils.py", line 208, in default
    return encoding_method(obj)
  File "C:\Python27\lib\site-packages\plotly\utils.py", line 270, in encode_as_datetime
    obj = obj.astimezone(pytz.utc)
  File "tslib.pyx", line 288, in pandas.tslib.Timestamp.tz_convert (pandas\tslib.c:7178)
Exception: Cannot convert tz-naive Timestamp, use tz_localize to localize

1 个答案:

答案 0 :(得分:0)

堆栈跟踪中的最后一个条目表示您的时间戳缺少时区信息。见http://pandas.pydata.org/pandas-docs/stable/timeseries.html#time-zone-handling

请特别注意页面底部附近In [301]:的tz_localize。