Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pull WEEKLY historical data from yahoo finance?

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?

like image 978
mlo Avatar asked Dec 14 '13 14:12

mlo


People also ask

Can you Download historical data from Yahoo Finance?

Save historical data from a desktop browser Click Historical Data. Select a Time Period, data to Show, and Frequency. Click Apply. To use the data offline, click Download.

How do I automatically pull data from Yahoo Finance to Excel?

Yahoo! Finance provides the simplest way to import financial data into a spreadsheet. The data (including stock prices, indices and company fundamentals) can be automatically downloaded in a CSV by simply entering a URL into your browser's address bar. The CSV can then be opened in Excel and manipulated as required.


1 Answers

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.

like image 155
unutbu Avatar answered Oct 31 '22 01:10

unutbu