是否可以加快此代码的速度? (大熊猫系列的预处理)

问题描述 投票:0回答:1

矩阵乘法已经实现,但是在具有约1mio索引的数据帧上运行仍需要花费大量时间...我能做些什么?

此代码的目的是使时间序列像数据一样固定。

def func(series, d, thres=1e-5):
   '''
   Series could be a single pandas series or a Dataframe.
   d is a positive real number
   thres is also a positive real number
   '''
    # function below returns a List of Lists (every sublist only contains a single float number e.g. [[0.0002],[0.004],...])
    w=get_weights_for_series(d,thres) 

    width = len(w)-1 

    df={}
    for name in series.columns:
        seriesF, df_  =  series[[name]].fillna(method='ffill').dropna(), pd.Series()  

        for iloc1 in range(width, seriesF.shape[0]):
            loc0, loc1 = seriesF.index[ iloc1-width ], seriesF.index[iloc1]
            if not np.isfinite(series.loc[loc1,name]):  
                continue
            df_[loc1]= np.dot(w.T, seriesF.loc[loc0:loc1])[0,0]
        df[name]=df_.copy(deep=True)
        df=pd.concat(df,axis=1)
        return df 
pandas performance numpy processing-efficiency
1个回答
0
投票

如果要将非平稳时间序列srs设为固定时间序列,则可以应用微分,它与srs.diff()一样简单。

我还想指出,显示的当前代码仅在第一列上运行,因为该函数在for循环的第一次迭代之后立即返回。

© www.soinside.com 2019 - 2024. All rights reserved.