@@ -738,37 +738,20 @@ def test_pct_change(test_series, shuffle, periods, fill_method, limit):
738
738
order = np .random .RandomState (seed = 42 ).permutation (len (df ))
739
739
df = df .reindex (order ).reset_index (drop = True )
740
740
741
- manual_apply = []
742
- for k in keys :
743
- ind = df .loc [df .key == k , 'vals' ]
744
- manual_apply .append (ind .pct_change (periods = periods ,
745
- fill_method = fill_method ,
746
- limit = limit ))
747
- exp_vals = pd .concat (manual_apply , ignore_index = True )
748
- exp = pd .DataFrame (exp_vals .values , columns = ['A' ])
741
+ df = getattr (df .groupby ('key' ), fill_method )(limit = limit )
749
742
grp = df .groupby ('key' )
750
-
751
- def get_result (grp_obj ):
752
- return grp_obj .pct_change (periods = periods ,
753
- fill_method = fill_method ,
754
- limit = limit )
743
+ exp = grp ['vals' ].shift (0 ) / grp ['vals' ].shift (periods ) - 1
744
+ exp = exp .to_frame ('vals' )
755
745
756
746
if test_series :
757
- exp = exp .loc [:, 'A' ]
758
- grp = grp ['vals' ]
759
- result = get_result (grp )
760
- df .insert (0 , 'A' , result )
761
- result = df .sort_values (by = 'key' )
762
- result = result .loc [:, 'A' ]
763
- result = result .reset_index (drop = True )
764
- tm .assert_series_equal (result , exp )
747
+ result = grp ['vals' ].pct_change (periods = periods ,
748
+ fill_method = fill_method ,
749
+ limit = limit )
750
+ tm .assert_series_equal (result , exp .loc [:, 'vals' ])
765
751
else :
766
- result = get_result (grp )
767
- result = result .reset_index (drop = True )
768
- df .insert (0 , 'A' , result )
769
- result = df .sort_values (by = 'key' )
770
- result = result .loc [:, ['A' ]]
771
- result = result .reset_index (drop = True )
752
+ result = grp .pct_change (periods = periods ,
753
+ fill_method = fill_method ,
754
+ limit = limit )
772
755
tm .assert_frame_equal (result , exp )
773
756
774
757
0 commit comments