Skip to content

Commit d8a0427

Browse files
Chang Shewesm
Chang She
authored andcommitted
ENH: pct change for Series and DataFrame #1271
1 parent e5f3a0d commit d8a0427

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

pandas/core/generic.py

+27
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,33 @@ def tshift(self, periods=1, freq=None, **kwds):
367367

368368
return self.shift(periods, freq, **kwds)
369369

370+
def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None,
371+
**kwds):
372+
"""
373+
Percent change over given number of periods
374+
375+
Parameters
376+
----------
377+
periods : int, default 1
378+
Periods to shift for forming percent change
379+
fill_method : str, default 'pad'
380+
How to handle NAs before computing percent changes
381+
limit : int, default None
382+
The number of consecutive NAs to fill before stopping
383+
freq : DateOffset, timedelta, or offset alias string, optional
384+
Increment to use from time series API (e.g. 'M' or BDay())
385+
386+
Returns
387+
-------
388+
chg : Series or DataFrame
389+
"""
390+
if fill_method is None:
391+
data = self
392+
else:
393+
data = self.fillna(method=fill_method, limit=limit)
394+
rs = data / data.shift(periods=periods, freq=freq, **kwds) - 1
395+
return rs
396+
370397

371398
class NDFrame(PandasObject):
372399
"""

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,7 @@ def shift(self, periods=1, freq=None, **kwds):
23542354
----------
23552355
periods : int
23562356
Number of periods to move, can be positive or negative
2357-
freq : DateOffset, timedelta, or time rule string, optional
2357+
freq : DateOffset, timedelta, or offset alias string, optional
23582358
Increment to use from datetools module or time rule (e.g. 'EOM')
23592359
23602360
Returns

pandas/tests/test_frame.py

+17
Original file line numberDiff line numberDiff line change
@@ -4116,6 +4116,23 @@ def test_diff(self):
41164116
assert_series_equal(the_diff['A'],
41174117
self.tsframe['A'] - self.tsframe['A'].shift(1))
41184118

4119+
def test_pct_change(self):
4120+
rs = self.tsframe.pct_change(fill_method=None)
4121+
assert_frame_equal(rs, self.tsframe / self.tsframe.shift(1) - 1)
4122+
4123+
rs = self.tsframe.pct_change(2)
4124+
filled = self.tsframe.fillna(method='pad')
4125+
assert_frame_equal(rs, filled / filled.shift(2) - 1)
4126+
4127+
rs = self.tsframe.pct_change(fill_method='bfill', limit=1)
4128+
filled = self.tsframe.fillna(method='bfill', limit=1)
4129+
assert_frame_equal(rs, filled / filled.shift(1) - 1)
4130+
4131+
rs = self.tsframe.pct_change(freq='M')
4132+
filled = self.tsframe.fillna(method='pad')
4133+
assert_frame_equal(rs, filled / filled.shift(freq='M') - 1)
4134+
4135+
41194136
def test_shift(self):
41204137
# naive shift
41214138
shiftedFrame = self.tsframe.shift(5)

pandas/tests/test_series.py

+16
Original file line numberDiff line numberDiff line change
@@ -2606,6 +2606,22 @@ def test_diff(self):
26062606
# Just run the function
26072607
self.ts.diff()
26082608

2609+
def test_pct_change(self):
2610+
rs = self.ts.pct_change(fill_method=None)
2611+
assert_series_equal(rs, self.ts / self.ts.shift(1) - 1)
2612+
2613+
rs = self.ts.pct_change(2)
2614+
filled = self.ts.fillna(method='pad')
2615+
assert_series_equal(rs, filled / filled.shift(2) - 1)
2616+
2617+
rs = self.ts.pct_change(fill_method='bfill', limit=1)
2618+
filled = self.ts.fillna(method='bfill', limit=1)
2619+
assert_series_equal(rs, filled / filled.shift(1) - 1)
2620+
2621+
rs = self.ts.pct_change(freq='M')
2622+
filled = self.ts.fillna(method='pad')
2623+
assert_series_equal(rs, filled / filled.shift(freq='M') - 1)
2624+
26092625
def test_autocorr(self):
26102626
# Just run the function
26112627
self.ts.autocorr()

0 commit comments

Comments
 (0)