1

I'm trying to plot a simple time series chart, seeing how closing prices have changed day over day. This is my code:

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()

This is the error message that I get:

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'

When looking online, I tend to see examples of 'hardcoded' variables, but I don't get how to apply this to a dataframe column - I found that I should use strptime to identify what format my date column is, but I don't know if this has any effect (I get the same error if I comment out the convert_dates apply method).

Thanks in advance for any help here, and open to any suggestions to streamlining this approach.

1 Answer 1

2

Seems like a format problem, try this:

# %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()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Go figure, of course pandas has a built in method to convert to datetime haha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.