Book Image

Mastering Pandas for Finance

By : Michael Heydt
Book Image

Mastering Pandas for Finance

By: Michael Heydt

Overview of this book

Table of Contents (16 chapters)
Mastering pandas for Finance
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Comparing stocks to the S&P 500


The analyses until this point have been performed only between stocks. It is often useful to perform some of these against a market index such as the S&P 500. This will give a sense of how those stocks compare to movements in the overall market.

At the beginning of the chapter, we loaded the S&P 500 data for the same time period as the other stocks. To perform comparisons, we can perform the same calculations to derive the daily percentage change and cumulative returns on the index:

In [50]:
   sp_500_dpc = sp_500['Adj Close'].pct_change().fillna(0)
   sp_500_dpc[:5]

Out[50]:
   Date
   2012-01-03    0.000
   2012-01-04    0.000
   2012-01-05    0.003
   2012-01-06   -0.003
   2012-01-09    0.002
   Name: Adj Close, dtype: float64

We can concatenate the index calculations in the results of the calculations of the stocks. This will let us easily compare the overall set of stocks and index calculations:

In [51]:
   dpc_all = pd.concat([sp_500_dpc...