Skip to content
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

[#7292] BUG: asfreq / pct_change strange behavior #19410

Merged
merged 13 commits into from
Jan 31, 2018
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ Datetimelike
- Bug in ``.astype()`` to non-ns timedelta units would hold the incorrect dtype (:issue:`19176`, :issue:`19223`, :issue:`12425`)
- Bug in subtracting :class:`Series` from ``NaT`` incorrectly returning ``NaT`` (:issue:`19158`)
- Bug in :func:`Series.truncate` which raises ``TypeError`` with a monotonic ``PeriodIndex`` (:issue:`17717`)
- Bug in :func:`~DataFrame.pct_change` using ``periods`` and ``freq`` returned different length outputs (:issue:`7292`)

Timezones
^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7315,6 +7315,7 @@ def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None,

rs = (data.div(data.shift(periods=periods, freq=freq, axis=axis,
**kwargs)) - 1)
rs = rs.reindex_like(data)
if freq is None:
mask = isna(com._values_from_object(self))
np.putmask(rs.values, mask, np.nan)
Expand Down
36 changes: 35 additions & 1 deletion pandas/tests/frame/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ def test_pct_change(self):

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

def test_pct_change_shift_over_nas(self):
s = Series([1., 1.5, np.nan, 2.5, 3.])
Expand All @@ -120,6 +122,38 @@ def test_pct_change_shift_over_nas(self):
edf = DataFrame({'a': expected, 'b': expected})
assert_frame_equal(chg, edf)

def test_pct_change_periods_freq(self):
# GH 7292
rs_freq = self.tsframe.pct_change(freq='5B')
rs_periods = self.tsframe.pct_change(5)
assert_frame_equal(rs_freq, rs_periods)

rs_freq = self.tsframe.pct_change(freq='3B', fill_method=None)
rs_periods = self.tsframe.pct_change(3, fill_method=None)
assert_frame_equal(rs_freq, rs_periods)

rs_freq = self.tsframe.pct_change(freq='3B', fill_method='bfill')
rs_periods = self.tsframe.pct_change(3, fill_method='bfill')
assert_frame_equal(rs_freq, rs_periods)

rs_freq = self.tsframe.pct_change(freq='7B',
fill_method='pad',
limit=1)
rs_periods = self.tsframe.pct_change(7, fill_method='pad', limit=1)
assert_frame_equal(rs_freq, rs_periods)

rs_freq = self.tsframe.pct_change(freq='7B',
fill_method='bfill',
limit=3)
rs_periods = self.tsframe.pct_change(7, fill_method='bfill', limit=3)
assert_frame_equal(rs_freq, rs_periods)

empty_ts = DataFrame(index=self.tsframe.index,
columns=self.tsframe.columns)
rs_freq = empty_ts.pct_change(freq='14B')
rs_periods = empty_ts.pct_change(14)
assert_frame_equal(rs_freq, rs_periods)

def test_frame_ctor_datetime64_column(self):
rng = date_range('1/1/2000 00:00:00', '1/1/2000 1:59:50', freq='10s')
dates = np.asarray(rng)
Expand Down
31 changes: 30 additions & 1 deletion pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ def test_pct_change(self):

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

def test_pct_change_shift_over_nas(self):
s = Series([1., 1.5, np.nan, 2.5, 3.])
Expand All @@ -353,6 +355,33 @@ def test_pct_change_shift_over_nas(self):
expected = Series([np.nan, 0.5, np.nan, 2.5 / 1.5 - 1, .2])
assert_series_equal(chg, expected)

def test_pct_change_periods_freq(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comments as above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added additional test cases and test when frame is empty.

# GH 7292
rs_freq = self.ts.pct_change(freq='5B')
rs_periods = self.ts.pct_change(5)
assert_series_equal(rs_freq, rs_periods)

rs_freq = self.ts.pct_change(freq='3B', fill_method=None)
rs_periods = self.ts.pct_change(3, fill_method=None)
assert_series_equal(rs_freq, rs_periods)

rs_freq = self.ts.pct_change(freq='3B', fill_method='bfill')
rs_periods = self.ts.pct_change(3, fill_method='bfill')
assert_series_equal(rs_freq, rs_periods)

rs_freq = self.ts.pct_change(freq='7B', fill_method='pad', limit=1)
rs_periods = self.ts.pct_change(7, fill_method='pad', limit=1)
assert_series_equal(rs_freq, rs_periods)

rs_freq = self.ts.pct_change(freq='7B', fill_method='bfill', limit=3)
rs_periods = self.ts.pct_change(7, fill_method='bfill', limit=3)
assert_series_equal(rs_freq, rs_periods)

empty_ts = Series(index=self.ts.index)
rs_freq = empty_ts.pct_change(freq='14B')
rs_periods = empty_ts.pct_change(14)
assert_series_equal(rs_freq, rs_periods)

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