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`` produces different sizes frames/series (:issue:`7292`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Change "produces different sizes frames/series" to "returned different length outputs"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

corrected.


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
9 changes: 8 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,11 @@ 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):
rs_periods = self.tsframe.pct_change(5)
Copy link
Contributor

Choose a reason for hiding this comment

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

add the issue number as a comment. Can you add a few more test cases here (via parametrization if possible). include an empty frame

rs_freq = self.tsframe.pct_change(periods=1, freq='5B')
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
9 changes: 8 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,11 @@ 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.

rs_periods = self.ts.pct_change(5)
rs_freq = self.ts.pct_change(periods=1, freq='5B')
assert_series_equal(rs_freq, rs_periods)

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