Notebook

Analyzing Data with Pandas - Time Series

In [8]:
% matplotlib inline
In [35]:
import matplotlib.pyplot as plt
import datetime
import pandas as pd

Read the data

In [36]:
yhoo = DataReader("yhoo", "yahoo", datetime.datetime(2007, 1, 1), 
    datetime.datetime(2012,1,1))

Plot stock price and volume

In [37]:
top = plt.subplot2grid((4,4), (0, 0), rowspan=3, colspan=4)
top.plot(yhoo.index, yhoo["Close"])
plt.title('Yahoo Price from 2007 - 2012')

bottom = plt.subplot2grid((4,4), (3,0), rowspan=1, colspan=4)
bottom.bar(yhoo.index, yhoo['Volume'])
plt.title('Yahoo Trading Volume')

plt.gcf().set_size_inches(15,8)

Calculate moving averages

In [38]:
mavg = yhoo['30_MA_Open'] = pd.stats.moments.rolling_mean(yhoo['Open'], 30)
yhoo['30_MA_Open'].tail() 
Out[38]:
Date
2011-12-23    15.593000
2011-12-27    15.599667
2011-12-28    15.595000
2011-12-29    15.595667
2011-12-30    15.608333
Name: 30_MA_Open, dtype: float64

Look at selected rows

In [39]:
yhoo[160:165]
Out[39]:
Open High Low Close Volume Adj Close 30_MA_Open 150_MA_Open
Date
2007-08-22 23.22 23.52 23.18 23.23 18763700 23.23 24.354000 28.316600
2007-08-23 23.35 23.36 22.95 23.13 15603000 23.13 24.242333 28.286067
2007-08-24 23.03 23.73 23.03 23.59 11191100 23.59 24.114333 28.253933
2007-08-27 23.59 23.76 23.01 23.03 16523800 23.03 24.018000 28.228400
2007-08-28 22.95 23.10 22.50 22.52 18030600 22.52 23.891667 28.192467

Index into a particular date

In [40]:
yhoo.ix['2010-01-04']
Out[40]:
Open                 16.940000
High                 17.200000
Low                  16.880000
Close                17.100000
Volume         16587400.000000
Adj Close            17.100000
30_MA_Open           15.806333
150_MA_Open          15.917600
Name: 2010-01-04 00:00:00, dtype: float64

Look at volume for the time period

In [41]:
yhoo.Volume.plot()
Out[41]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faf381d13d0>

More plots

In [42]:
yhoo.plot(subplots = True, figsize = (8, 8));
plt.legend(loc = 'best')
plt.show()
In [43]:
close_px = yhoo['Adj Close']
mavg = pd.rolling_mean(close_px, 30)

Moving average plot

In [44]:
yhoo.Close.plot(label='Yahoo')
mavg.plot(label='mavg')
plt.legend()
plt.gcf().set_size_inches(15,8)

KDE plot

In [45]:
yhoo.Close.plot(kind='kde')
Out[45]:
<matplotlib.axes._subplots.AxesSubplot at 0x7faf260320d0>