Skip to content

Commit c2f8f68

Browse files
committed
Created pct_change function
1 parent 63ce781 commit c2f8f68

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

pandas/core/groupby.py

+17
Original file line numberDiff line numberDiff line change
@@ -2044,6 +2044,23 @@ def shift(self, periods=1, freq=None, axis=0):
20442044
result_is_index=True,
20452045
periods=periods)
20462046

2047+
@Substitution(name='groupby')
2048+
@Appender(_doc_template)
2049+
def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None,
2050+
axis=0):
2051+
"""Calcuate pct_change of each value to previous entry in group"""
2052+
if freq is not None or axis != 0:
2053+
return self.apply(lambda x: x.pct_change(periods=periods,
2054+
fill_method=fill_method,
2055+
limit=limit, freq=freq,
2056+
axis=axis))
2057+
2058+
filled = getattr(self, fill_method)(limit=limit).drop(
2059+
self.grouper.names, axis=1)
2060+
shifted = filled.shift(periods=periods, freq=freq)
2061+
2062+
return (filled / shifted) - 1
2063+
20472064
@Substitution(name='groupby')
20482065
@Appender(_doc_template)
20492066
def head(self, n=5):

0 commit comments

Comments
 (0)