Skip to content

ENH: pct change for Series and DataFrame #1271 #1281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,33 @@ def tshift(self, periods=1, freq=None, **kwds):

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

def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None,
**kwds):
"""
Percent change over given number of periods

Parameters
----------
periods : int, default 1
Periods to shift for forming percent change
fill_method : str, default 'pad'
How to handle NAs before computing percent changes
limit : int, default None
The number of consecutive NAs to fill before stopping
freq : DateOffset, timedelta, or offset alias string, optional
Increment to use from time series API (e.g. 'M' or BDay())

Returns
-------
chg : Series or DataFrame
"""
if fill_method is None:
data = self
else:
data = self.fillna(method=fill_method, limit=limit)
rs = data / data.shift(periods=periods, freq=freq, **kwds) - 1
return rs


class NDFrame(PandasObject):
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2345,7 +2345,7 @@ def shift(self, periods=1, freq=None, **kwds):
----------
periods : int
Number of periods to move, can be positive or negative
freq : DateOffset, timedelta, or time rule string, optional
freq : DateOffset, timedelta, or offset alias string, optional
Increment to use from datetools module or time rule (e.g. 'EOM')

Returns
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4116,6 +4116,23 @@ def test_diff(self):
assert_series_equal(the_diff['A'],
self.tsframe['A'] - self.tsframe['A'].shift(1))

def test_pct_change(self):
rs = self.tsframe.pct_change(fill_method=None)
assert_frame_equal(rs, self.tsframe / self.tsframe.shift(1) - 1)

rs = self.tsframe.pct_change(2)
filled = self.tsframe.fillna(method='pad')
assert_frame_equal(rs, filled / filled.shift(2) - 1)

rs = self.tsframe.pct_change(fill_method='bfill', limit=1)
filled = self.tsframe.fillna(method='bfill', limit=1)
assert_frame_equal(rs, filled / filled.shift(1) - 1)

rs = self.tsframe.pct_change(freq='M')
filled = self.tsframe.fillna(method='pad')
assert_frame_equal(rs, filled / filled.shift(freq='M') - 1)


def test_shift(self):
# naive shift
shiftedFrame = self.tsframe.shift(5)
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2596,6 +2596,22 @@ def test_diff(self):
# Just run the function
self.ts.diff()

def test_pct_change(self):
rs = self.ts.pct_change(fill_method=None)
assert_series_equal(rs, self.ts / self.ts.shift(1) - 1)

rs = self.ts.pct_change(2)
filled = self.ts.fillna(method='pad')
assert_series_equal(rs, filled / filled.shift(2) - 1)

rs = self.ts.pct_change(fill_method='bfill', limit=1)
filled = self.ts.fillna(method='bfill', limit=1)
assert_series_equal(rs, filled / filled.shift(1) - 1)

rs = self.ts.pct_change(freq='M')
filled = self.ts.fillna(method='pad')
assert_series_equal(rs, filled / filled.shift(freq='M') - 1)

def test_autocorr(self):
# Just run the function
self.ts.autocorr()
Expand Down