O'Reilly logo

Mastering pandas for Finance by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required