How do you pull WEEKLY historical data from yahoo finance?

mlo picture mlo · Dec 14, 2013 · Viewed 8.3k times · Source
import datetime   
import pandas.io.data

sp  =  pd.io.data.get_data_yahoo('^IXIC',start = datetime.datetime(1972, 1, 3),
                       end = datetime.datetime(2010, 1, 3))

I have used the above example, but that just pulls DAILY data into a dataframe when I would like to pull weekly. It doesn't seem like get_data_yahoo has a parameter where you can select perhaps from daily, weekly or monthly like the options made available on yahoo itself. Any other packages or ideas that you know of that might be able to facilitate this?

Answer

unutbu picture unutbu · Dec 14, 2013

You can downsample using the asfreq method:

sp = sp.asfreq('W-FRI', method='pad')

The pad method will propagate the last valid observation forward.

Using resample (as @tshauck has shown) is another possibility. Use asfreq if you want to guarantee that the values in your downsample are values found in the original data set. Use resample if you wish to aggregate groups of rows from the original data set (for example, by taking a mean). reindex might introduce NaN values if the original data set does not have a value on the date specified by the reindex -- though (as @behzad.nouri points out) you could use method=pad to propagate last observations here as well.