Search code examples
pandaspandas-datareader

How to pull EOD stock data from yahoo finance for excatly last 20 WORKING Days using Pandas in Python 2.7


Right now what I am doing is to pull data for the last 30 days, store this in a dataframe and then pick the data for the last 20 days to use. However If one of the days in the last 20 days is a holiday, then Yahoo shows the Volume across that day as 0 and fills the OHLC(Open, High, Low, Close, Adj Close) with the Adj Close of the previous day. In the example shown below, the data for 2016-01-26 is invalid and I dont want to retreive this data. So how do I pull data from Yahoo for excatly the last 20 working days ? My present code is below:

from datetime import date, datetime, timedelta
import pandas_datareader.data as web
todays_date = date.today()
n = 30
date_n_days_ago = date.today() - timedelta(days=n)
yahoo_data = web.DataReader('ACC.NS', 'yahoo', date_n_days_ago, todays_date)
yahoo_data_20_day = yahoo_data.tail(20)

Solution

  • IIUC you can add filter, where column Volume is not 0:

    from datetime import date, datetime, timedelta
    import pandas_datareader.data as web
    
    todays_date = date.today()
    n = 30
    date_n_days_ago = date.today() - timedelta(days=n)
    yahoo_data = web.DataReader('ACC.NS', 'yahoo', date_n_days_ago, todays_date)
    
    #add filter - get data, where column Volume is not 0
    yahoo_data = yahoo_data[yahoo_data.Volume != 0]
    
    yahoo_data_20_day = yahoo_data.tail(20)
    
    print yahoo_data_20_day
                   Open     High      Low    Close  Volume  Adj Close
    Date                                                             
    2016-01-20  1218.90  1229.00  1205.00  1212.25  156300    1206.32
    2016-01-21  1225.00  1236.95  1211.25  1228.45  209200    1222.44
    2016-01-22  1239.95  1256.65  1230.05  1241.00  123200    1234.93
    2016-01-25  1250.00  1263.50  1241.05  1245.00  124500    1238.91
    2016-01-27  1249.00  1250.00  1228.00  1230.35  112800    1224.33
    2016-01-28  1232.40  1234.90  1208.00  1214.95  134500    1209.00
    2016-01-29  1220.10  1253.50  1216.05  1240.05  254400    1233.98
    2016-02-01  1245.00  1278.90  1240.30  1271.85  210900    1265.63
    2016-02-02  1266.80  1283.00  1253.05  1261.35  204600    1255.18
    2016-02-03  1244.00  1279.00  1241.45  1248.95  191000    1242.84
    2016-02-04  1255.25  1277.40  1253.20  1270.40  205900    1264.18
    2016-02-05  1267.05  1286.00  1259.05  1271.40  231300    1265.18
    2016-02-08  1271.00  1309.75  1270.15  1280.60  218500    1274.33
    2016-02-09  1271.00  1292.85  1270.00  1279.10  148600    1272.84
    2016-02-10  1270.00  1278.25  1250.05  1265.85  256800    1259.66
    2016-02-11  1250.00  1264.70  1225.50  1234.00  231500    1227.96
    2016-02-12  1234.20  1242.65  1199.10  1221.05  212000    1215.07
    2016-02-15  1230.00  1268.70  1228.35  1256.55  130800    1250.40
    2016-02-16  1265.00  1273.10  1225.00  1227.80  144700    1221.79
    2016-02-17  1222.80  1233.50  1204.00  1226.05  165000    1220.05