【对比Python】分组子集连续区间比上期

任务:计算各股票最长连涨天数

Python

1 import pandas as pd
2 def con_rise(stock:pd.DataFrame):
3     rise_day_list = []
4     rise_num = 0
5     shift_1 = stock['CL']>stock['CL'].shift(1)
6     for bl in shift_1:
7         if bl == False:
8             rise_num = 0
9         else:
10             rise_num+=1
11         rise_day_list.append(rise_num)
12     return max(rise_day_list)
13 stock_file = "E:\\txt\\StockRecords.txt"
14 stock_info = pd.read_csv(stock_file,sep="\t")
15 stock_info.sort_values(by='DT',inplace=True)
16 stock_group = stock_info.groupby(by='CODE')
17 max_rise_list = []
18 for index,stock_g in stock_group:
19     code = stock_g.iloc[0]['CODE']
20     max_rise_list.append([code,con_rise(stock_g)])
21 max_rise_df = pd.DataFrame(max_rise_list,columns=['CODE','con_rise'])   
22 print(max_rise_df)

集算器

  A B  
1 E:\\txt\\StockRecords.txt    
2 =file(A1).import@t()    
3 =A2.sort(DT)    
4 =A3.group(CODE)    
5 =A4.new(CODE,func(A6,~):con_rise)    
6 func    
7   =(num=0,A6.max(num=if(CL>CL[-1],if(#==1,0,num+1),0)))  

单支股票的连续区间比上期思路:如果大于前一天的股价则加1,不大于则置0,最后查看序列中的最大值即可。把单支股票的计算方式写成函数,每支股票的表当做参数传入即可。集算器可以方便的在循环函数中调用函数得到结果。同样的思路,Pandas的代码看起来就复杂的多。