@@ -729,15 +729,13 @@ def interweave(list_obj):
729
729
(- 1 , 'ffill' , None ), (- 1 , 'ffill' , 1 ),
730
730
(- 1 , 'bfill' , None ), (- 1 , 'bfill' , 1 )])
731
731
def test_pct_change (test_series , shuffle , periods , fill_method , limit ):
732
- # Groupby pct change uses an apply if monotonic
733
- # and a vectorized operation if non-monotonic
734
- # Shuffle parameter tests each
735
- vals = [np .nan , np .nan , 1 , 2 , 4 , 10 , np .nan , np .nan ]
732
+ vals = [3 , np .nan , 1 , 2 , 4 , 10 , np .nan , np .nan ]
736
733
keys = ['a' , 'b' ]
737
734
key_v = [k for j in list (map (lambda x : [x ] * len (vals ), keys )) for k in j ]
738
735
df = DataFrame ({'key' : key_v , 'vals' : vals * 2 })
739
736
if shuffle :
740
- df = df .reindex (np .random .permutation (len (df ))).reset_index (drop = True )
737
+ order = np .random .RandomState (seed = 42 ).permutation (len (df ))
738
+ df = df .reindex (order ).reset_index (drop = True )
741
739
742
740
manual_apply = []
743
741
for k in keys :
@@ -746,30 +744,27 @@ def test_pct_change(test_series, shuffle, periods, fill_method, limit):
746
744
fill_method = fill_method ,
747
745
limit = limit ))
748
746
exp_vals = pd .concat (manual_apply ).reset_index (drop = True )
749
- exp = pd .DataFrame (exp_vals , columns = ['_pct_change ' ])
747
+ exp = pd .DataFrame (exp_vals , columns = ['A ' ])
750
748
grp = df .groupby ('key' )
751
749
752
750
def get_result (grp_obj ):
753
751
return grp_obj .pct_change (periods = periods ,
754
752
fill_method = fill_method ,
755
753
limit = limit )
756
754
757
- # Specifically test when monotonic and not monotonic
758
-
759
755
if test_series :
760
- exp = exp .loc [:, '_pct_change ' ]
756
+ exp = exp .loc [:, 'A ' ]
761
757
grp = grp ['vals' ]
762
758
result = get_result (grp )
763
- # Resort order by keys to compare to expected values
764
- df .insert (0 , '_pct_change' , result )
759
+ df .insert (0 , 'A' , result )
765
760
result = df .sort_values (by = 'key' )
766
- result = result .loc [:, '_pct_change ' ]
761
+ result = result .loc [:, 'A ' ]
767
762
result = result .reset_index (drop = True )
768
763
tm .assert_series_equal (result , exp )
769
764
else :
770
765
result = get_result (grp )
771
766
result .reset_index (drop = True , inplace = True )
772
- result .columns = ['_pct_change ' ]
767
+ result .columns = ['A ' ]
773
768
tm .assert_frame_equal (result , exp )
774
769
775
770
0 commit comments