你可以从这样的方法开始
import pandas as pd
import numpy as np
from datetime import datetime
from pandas.tseries.offsets import MonthEnd
#sample Data
df = pd.DataFrame(index=pd.DatetimeIndex(freq='D',start=datetime(1990,1,1),end=datetime(1995,12,25)),data=np.random.randint(0,100,(2185,3)),columns=['Stock A','Stock B','Stock C'])
#Create a column that has the end of the month for each date
df['end'] = df.index + MonthEnd(1)
#Groupby the end of the month and apply your regression function
for group,data in df.groupby('end'):
for row in data.columns #Sudo code loop over columns used per regression
regressFunction()
这将消除浪费在行和列上的循环时间,而只需计算已知索引上的回归。此外,在
parallel
加快计算速度。