使用cast string>float typ对数据进行过滤

2024-10-02 22:23:08 发布

您现在位置:Python中文网/ 问答频道 /正文

这里有一些问题,但我认为代码是相对直接的。你知道吗

代码如下:

        import pandas as 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
            now = 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 one day return
        underlying = establishAdjustmentFactor(calculateLongReturn(underlying));

        #filter here
        underlying = underlying[(underlying['Date'] > long(argStartYear + argStartMonth +  argStartDay)) & underlying['Date']<long(argEndYear+argEndMonth+argEndDay)];

其中,这将演变为一个函数,argStart(End)将是该函数的参数。你知道吗

其想法是,将有一些父函数调用,将保持一个全局数据帧的整个价格历史的基础上,后来的调用将访问该数据帧和过滤器的日期需要看看是否有分裂。你知道吗

现在,当我读取数据并尝试在read_csv调用中转换时,我得到以下错误:

            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 for 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

任何关于为什么的意见都会非常有用!你知道吗


Tags: indfclosedatadatereturndefline
1条回答
网友
1楼 · 发布于 2024-10-02 22:23:08

因此,似乎我已经发现了这个问题后,拨弄了一点,并改变了我的方式思考问题。你知道吗

如果有一个更有效的方法来做这件事,任何投入都将是巨大的。你知道吗

    def operateOverSetToCreateEasyKey(df):
        for i in df.index:
            df.ix[i,'fmtDate']=int(str(df.ix[i]['Date']).replace('-',''));
        return df;

相关问题 更多 >