python - Filtering on data with a cast string->float type -
a few issues here think code relatively straight forward.
the code follows:
import pandas pd def establishadjustmentfactor(df): df['adjfactor']=df['adj close']/df['close']; df['chgfactor']=df['adjfactor']/df['adjfactor'].shift(1); return df; def yahoofinanceaccessor(ticker,year_,month_,day_): import datetime = datetime.datetime.now() month = str(int(now.strftime("%m"))-1) day = str(int(now.strftime("%d"))+1) year = str(int(now.strftime("%y"))) data = pd.read_csv('/users/mydir/downloads/' + ticker + '.csv'); data['date']=float(str(data['date']).replace('-','')); data.set_index('date') data=data.sort(['date'],ascending=[1]); return data def calculatelongreturn(df): df['ret']=df['adj close'].pct_change(); return df; argstartyear = '2014'; argstartmonth = '01'; argstartday='01'; argendyear = '2014'; argendmonth = '04'; argendday = '30'; #read data underlying = yahoofinanceaccessor("ibm,"1900","01","01"); #get 1 day return underlying = establishadjustmentfactor(calculatelongreturn(underlying)); #filter here underlying = underlying[(underlying['date'] > long(argstartyear + argstartmonth + argstartday)) & underlying['date']<long(argendyear+argendmonth+argendday)];
where evolve function , argstart(end) arguments function.
the idea there parent function call keep global dataframe of entire price history of underlying, , later calls access dataframe , filter on dates needed see if there splits.
now, when read data , attempt convert in read_csv
call following error:
traceback (most recent call last): file "<stdin>", line 1, in <module> file "/applications/spyder.app/contents/resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile execfile(filename, namespace) file "/users/mydir/documents/pythonprojects/dailyoptionvaluation.py", line 70, in <module> underlying = yahoofinanceaccessor("svxy","1900","01","01"); file "/users/mydir/documents/pythonprojects/dailyoptionvaluation.py", line 37, in yahoofinanceaccessor data['date']=float(str(data['date']).replace('-','')); valueerror: invalid literal float(): 0 20140424 1 20140423 2 20140422 3 20140421 4 20140417 5 20140416 6 20140415 7 20140414 8 20140411 9 20140410 10 20140409 11 20140408 12 20140407
any input why extremely helpful!
so appears if have found issue after poking around little bit, , changing way thinking problem.
any input on if there more efficient way great.
def operateoversettocreateeasykey(df): in df.index: df.ix[i,'fmtdate']=int(str(df.ix[i]['date']).replace('-','')); return df;
Comments
Post a Comment