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 used the above example, but it just pulls DAILY data into the data framework when I would like to pull weekly . It seems that get_data_yahoo has a parameter where you can select, perhaps daily, weekly or monthly, for example, the parameters available to yahoo itself. Any other packages or ideas you know about this could make this easier?

+6
source share
5 answers

You can reduce the use of the asfreq method:

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

The pad method will propagate the last valid observation forward.

Using resample (as @tshauck showed) is another option. Use asfreq if you want to ensure that the values ​​in your downsample are the values ​​found in the source dataset. Use resample if you want to combine row groups from the original dataset (for example, taking an average value). reindex can enter NaN values ​​if the original dataset does not have a date specified in reindex - although (as @ behzad.nouri points out) you could use method=pad to propagate the latest observations here.

+4
source

If you check the latest pandas source code on github, you will see that the interval parameter is included in the last leading branch. You can manually change the local copy by overwriting the same data.py file in the Site-Packages / pandas / io folder

+3
source

You can always translate to the desired frequency:

 sp.reindex( pd.date_range( start=sp.index.min( ), end=sp.index.max( ), freq='W-WED' ) ) # weekly, Wednesdays 

edit : you can add , method='ffill' to forward the NaN fill.

Take Wednesday as a suggestion, because they have the least missing values. (that is, fewer holidays in the NYSE fall on Wednesday). I think Yahoo's weekly data gives stock prices on Monday, which is the worst weekly frequency based on S&P data since 2000:

 import pandas.io.data as web sp = web.DataReader("^GSPC", "yahoo", start=dt.date( 2000, 1, 1 ) ) weekday = { 0:'MON', 1:'TUE', 2:'WED', 3:'THU', 4:'FRI' } sp[ 'weekday' ] = list( map( weekday.get, sp.index.dayofweek ) ) sp.weekday.value_counts( ) 

output:

 WED 722 TUE 717 THU 707 FRI 705 MON 659 
+2
source

One option is to disguise on the desired day of the week.

 sp[sp.index.dayofweek == 0] 

Another option would be re-fetching.

 sp.resample('W', how='mean') 
+1
source

What I convert daily into weekly price data:

 import datetime import pandas as pd import pandas_datareader.data as web start = datetime.datetime(1972, 1, 3) end = datetime.datetime(2010, 1, 3) stock_d = web.DataReader('^IXIC', 'yahoo', start, end) def week_open(array_like): return array_like[0] def week_close(array_like): return array_like[-1] stock_w = stock_d.resample('W', how={'Open': week_open, 'High': 'max', 'Low': 'min', 'Close': week_close, 'Volume': 'sum'}, loffset=pd.offsets.timedelta(days=-6)) stock_w = stock_w[['Open', 'High', 'Low', 'Close', 'Volume']] 

Additional Information:

https://pandas-datareader.readthedocs.io/en/latest/remote_data.html#yahoo-finance https://gist.github.com/prithwi/339f87bf9c3c37bb3188

0
source

All Articles