@@ -723,30 +723,37 @@ def interweave(list_obj):
723
723
724
724
@pytest .mark .parametrize ("test_series" , [True , False ])
725
725
@pytest .mark .parametrize ("shuffle" , [True , False ])
726
+ @pytest .mark .parametrize ("activate_cache" , [True , False ])
726
727
@pytest .mark .parametrize ("periods,fill_method,limit" , [
727
728
(1 , 'ffill' , None ), (1 , 'ffill' , 1 ),
728
729
(1 , 'bfill' , None ), (1 , 'bfill' , 1 ),
729
730
(- 1 , 'ffill' , None ), (- 1 , 'ffill' , 1 ),
730
- (- 1 , 'bfill' , None ), (- 1 , 'bfill' , 1 )])
731
- def test_pct_change (test_series , shuffle , periods , fill_method , limit ):
732
- vals = [3 , np .nan , 1 , 2 , 4 , 10 , np .nan , np .nan ]
731
+ (- 1 , 'bfill' , None ), (- 1 , 'bfill' , 1 ),
732
+ (- 1 , None , None ), (- 1 , None , 1 ),
733
+ (- 1 , None , None ), (- 1 , None , 1 )
734
+ ])
735
+ def test_pct_change (test_series , shuffle , activate_cache , periods , fill_method , limit ):
736
+ vals = [3 , np .nan , 1 , 2 , 4 , 10 , np .nan , 9 ]
733
737
keys = ['a' , 'b' ]
734
- key_v = [ k for j in list ( map ( lambda x : [ x ] * len (vals ), keys )) for k in j ]
738
+ key_v = np . repeat ( keys , len (vals ))
735
739
df = DataFrame ({'key' : key_v , 'vals' : vals * 2 })
736
740
if shuffle :
737
741
order = np .random .RandomState (seed = 42 ).permutation (len (df ))
738
742
df = df .reindex (order ).reset_index (drop = True )
739
743
740
744
manual_apply = []
741
745
for k in keys :
742
- subgroup = Series ( df .loc [df .key == k , 'vals' ]. values )
743
- manual_apply .append (subgroup .pct_change (periods = periods ,
744
- fill_method = fill_method ,
745
- limit = limit ))
746
- exp_vals = pd .concat (manual_apply ). reset_index ( drop = True )
747
- exp = pd .DataFrame (exp_vals , columns = ['A' ])
746
+ ind = df .loc [df .key == k , 'vals' ]
747
+ manual_apply .append (ind .pct_change (periods = periods ,
748
+ fill_method = fill_method ,
749
+ limit = limit ))
750
+ exp_vals = pd .concat (manual_apply , ignore_index = True )
751
+ exp = pd .DataFrame (exp_vals . values , columns = ['A' ])
748
752
grp = df .groupby ('key' )
749
753
754
+ if activate_cache :
755
+ grp .grouper .is_monotonic
756
+
750
757
def get_result (grp_obj ):
751
758
return grp_obj .pct_change (periods = periods ,
752
759
fill_method = fill_method ,
@@ -763,7 +770,7 @@ def get_result(grp_obj):
763
770
tm .assert_series_equal (result , exp )
764
771
else :
765
772
result = get_result (grp )
766
- result .reset_index (drop = True , inplace = True )
773
+ result = result .reset_index (drop = True )
767
774
result .columns = ['A' ]
768
775
tm .assert_frame_equal (result , exp )
769
776
0 commit comments