Book Image

Learning NumPy Array

By : Ivan Idris
Book Image

Learning NumPy Array

By: Ivan Idris

Overview of this book

Table of Contents (14 chapters)
Learning NumPy Array
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Correlating weather and stocks with pandas


We will try to correlate stock market data for the Netherlands with the DataFrame we produced last time from the KNMI De Bilt weather data. As a proxy for the stock market, we will use closing prices of the EWN ETF. This might not be the best choice, by the way, so if you have a better idea, please use the appropriate stock ticker. The steps for this exercise are provided as follows:

  1. Download the EWN data from Yahoo Finance, with a special function. The code is as follows:

    #EWN start Mar 22, 1996
    start = dt(1996, 3, 22)
    end = dt(2013, 5, 4)
    
    symbol = "EWN"
    quotes = finance.quotes_historical_yahoo(symbol, start, end, asobject=True)
  2. Create a DataFrame object with the available dates in the downloaded data:

    df2 = pd.DataFrame(quotes.close, index=dt_idx, columns=[symbol])
  3. Join the new DataFrame object with DataFrame of the weather data. We will then obtain the correlation matrix:

    df3 = df.join(df2)
    
    print df3.corr()

    The correlation matrix is as follows:

As...