@@ -2070,10 +2070,24 @@ def shift(self, periods=1, freq=None, axis=0):
2070
2070
def pct_change (self , periods = 1 , fill_method = 'pad' , limit = None , freq = None ,
2071
2071
axis = 0 ):
2072
2072
"""Calcuate pct_change of each value to previous entry in group"""
2073
- return self .apply (lambda x : x .pct_change (periods = periods ,
2074
- fill_method = fill_method ,
2075
- limit = limit , freq = freq ,
2076
- axis = axis ))
2073
+ if freq is not None or axis != 0 :
2074
+ return self .apply (lambda x : x .pct_change (periods = periods ,
2075
+ fill_method = fill_method ,
2076
+ limit = limit , freq = freq ,
2077
+ axis = axis ))
2078
+ if fill_method :
2079
+ new = DataFrameGroupBy (self ._obj_with_exclusions ,
2080
+ grouper = self .grouper )
2081
+ new .obj = getattr (new , fill_method )(limit = limit )
2082
+ new ._reset_cache ()
2083
+ else :
2084
+ new = self
2085
+
2086
+ obj = new .obj .drop (self .grouper .names , axis = 1 )
2087
+ shifted = new .shift (periods = periods , freq = freq ).\
2088
+ drop (self .grouper .names , axis = 1 )
2089
+ return (obj / shifted ) - 1
2090
+
2077
2091
2078
2092
@Substitution (name = 'groupby' )
2079
2093
@Appender (_doc_template )
@@ -3936,9 +3950,15 @@ def _apply_to_column_groupbys(self, func):
3936
3950
3937
3951
def pct_change (self , periods = 1 , fill_method = 'pad' , limit = None , freq = None ):
3938
3952
"""Calcuate pct_change of each value to previous entry in group"""
3939
- return self .apply (lambda x : x .pct_change (periods = periods ,
3940
- fill_method = fill_method ,
3941
- limit = limit , freq = freq ))
3953
+ if fill_method :
3954
+ new = SeriesGroupBy (self .obj , grouper = self .grouper )
3955
+ new .obj = getattr (new , fill_method )(limit = limit )
3956
+ new ._reset_cache ()
3957
+ else :
3958
+ new = self
3959
+
3960
+ shifted = new .shift (periods = periods , freq = freq )
3961
+ return (new .obj / shifted ) - 1
3942
3962
3943
3963
3944
3964
class NDFrameGroupBy (GroupBy ):
0 commit comments