Skip to content

Commit 710d82c

Browse files
dongho-jungjreback
authored andcommitted
BUG: make pct_change can handle the anchored freq #28664 (#28681)
1 parent f7d6b58 commit 710d82c

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

doc/source/whatsnew/v1.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ Reshaping
440440
- :func:`qcut` and :func:`cut` now handle boolean input (:issue:`20303`)
441441
- Fix to ensure all int dtypes can be used in :func:`merge_asof` when using a tolerance value. Previously every non-int64 type would raise an erroneous ``MergeError`` (:issue:`28870`).
442442
- Better error message in :func:`get_dummies` when `columns` isn't a list-like value (:issue:`28383`)
443+
- Bug :meth:`Series.pct_change` where supplying an anchored frequency would throw a ValueError (:issue:`28664`)
443444

444445
Sparse
445446
^^^^^^

pandas/core/generic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10443,6 +10443,7 @@ def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None, **kwar
1044310443
data = self.fillna(method=fill_method, limit=limit, axis=axis)
1044410444

1044510445
rs = data.div(data.shift(periods=periods, freq=freq, axis=axis, **kwargs)) - 1
10446+
rs = rs.loc[~rs.index.duplicated()]
1044610447
rs = rs.reindex_like(data)
1044710448
if freq is None:
1044810449
mask = isna(com.values_from_object(data))

pandas/tests/series/test_timeseries.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,16 @@ def test_pct_change(self, datetime_series):
370370
rs, (filled / filled.shift(freq="5D") - 1).reindex_like(filled)
371371
)
372372

373+
def test_pct_change_with_duplicate_axis(self):
374+
# GH 28664
375+
common_idx = date_range("2019-11-14", periods=5, freq="D")
376+
result = Series(range(5), common_idx).pct_change(freq="B")
377+
378+
# the reason that the expected should be like this is documented at PR 28681
379+
expected = Series([np.NaN, np.inf, np.NaN, np.NaN, 3.0], common_idx)
380+
381+
tm.assert_series_equal(result, expected)
382+
373383
def test_pct_change_shift_over_nas(self):
374384
s = Series([1.0, 1.5, np.nan, 2.5, 3.0])
375385

0 commit comments

Comments
 (0)