@@ -494,15 +494,12 @@ Unlike agg, apply's callable is passed a sub-DataFrame which gives you access to
494494
495495 S = pd.Series([i / 100.0 for i in range (1 , 11 )])
496496
497-
498497 def cum_ret (x , y ):
499498 return x * (1 + y)
500499
501-
502500 def red (x ):
503501 return functools.reduce(cum_ret, x, 1.0 )
504502
505-
506503 S.expanding().apply(red, raw = True )
507504
508505
@@ -514,12 +511,10 @@ Unlike agg, apply's callable is passed a sub-DataFrame which gives you access to
514511 df = pd.DataFrame({" A" : [1 , 1 , 2 , 2 ], " B" : [1 , - 1 , 1 , 2 ]})
515512 gb = df.groupby(" A" )
516513
517-
518514 def replace (g ):
519515 mask = g < 0
520516 return g.where(mask, g[~ mask].mean())
521517
522-
523518 gb.transform(replace)
524519
525520 `Sort groups by aggregated data
@@ -551,13 +546,11 @@ Unlike agg, apply's callable is passed a sub-DataFrame which gives you access to
551546 rng = pd.date_range(start = " 2014-10-07" , periods = 10 , freq = " 2min" )
552547 ts = pd.Series(data = list (range (10 )), index = rng)
553548
554-
555549 def MyCust (x ):
556550 if len (x) > 2 :
557551 return x[1 ] * 1.234
558552 return pd.NaT
559553
560-
561554 mhc = {" Mean" : np.mean, " Max" : np.max, " Custom" : MyCust}
562555 ts.resample(" 5min" ).apply(mhc)
563556 ts
@@ -803,11 +796,9 @@ Apply
803796 index = [" I" , " II" , " III" ],
804797 )
805798
806-
807799 def SeriesFromSubList (aList ):
808800 return pd.Series(aList)
809801
810-
811802 df_orgz = pd.concat(
812803 {ind: row.apply(SeriesFromSubList) for ind, row in df.iterrows()}
813804 )
@@ -827,12 +818,10 @@ Rolling Apply to multiple columns where function calculates a Series before a Sc
827818 )
828819 df
829820
830-
831821 def gm (df , const ):
832822 v = ((((df[" A" ] + df[" B" ]) + 1 ).cumprod()) - 1 ) * const
833823 return v.iloc[- 1 ]
834824
835-
836825 s = pd.Series(
837826 {
838827 df.index[i]: gm(df.iloc[i: min (i + 51 , len (df) - 1 )], 5 )
@@ -859,11 +848,9 @@ Rolling Apply to multiple columns where function returns a Scalar (Volume Weight
859848 )
860849 df
861850
862-
863851 def vwap (bars ):
864852 return (bars.Close * bars.Volume).sum() / bars.Volume.sum()
865853
866-
867854 window = 5
868855 s = pd.concat(
869856 [
0 commit comments