Skip to content

Commit eaede34

Browse files
committed
BUG,TST: Remove case where vectorization fails in pct_change groupby method, improve test. #21235
1 parent 849fac4 commit eaede34

File tree

2 files changed

+5
-39
lines changed

2 files changed

+5
-39
lines changed

pandas/core/groupby/groupby.py

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3935,38 +3935,10 @@ def _apply_to_column_groupbys(self, func):
39353935
return func(self)
39363936

39373937
def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None):
3938-
"""Calculate pct_change of each value to previous entry in group"""
3939-
grouper = self.grouper
3940-
cache_exist = getattr(grouper, '_cache', False)
3941-
if cache_exist:
3942-
in_cache = True if 'is_monotonic' in cache_exist.keys() else False
3943-
else:
3944-
in_cache = False
3945-
m = grouper.is_monotonic if in_cache else False
3946-
if not m or fill_method is None:
3947-
return self.apply(lambda x: x.pct_change(periods=periods,
3948-
fill_method=fill_method,
3949-
limit=limit, freq=freq))
3950-
3951-
def get_invalid_index(x):
3952-
if periods == 0:
3953-
return x
3954-
elif periods > 0:
3955-
ax = Index(np.arange(min(x), min(x) + periods))
3956-
return ax
3957-
elif periods < 0:
3958-
ax = Index(np.arange(max(x), max(x) + periods, -1))
3959-
return ax
3960-
3961-
filled = getattr(self, fill_method)(limit=limit)
3962-
shifted = filled.shift(periods=periods, freq=freq)
3963-
pct_change = (filled / shifted) - 1
3964-
3965-
invalid_index = Index([])
3966-
for i in [get_invalid_index(v) for k, v in self.indices.items()]:
3967-
invalid_index = invalid_index.union(i)
3968-
pct_change.iloc[invalid_index] = np.nan
3969-
return pct_change
3938+
"""Calcuate pct_change of each value to previous entry in group"""
3939+
return self.apply(lambda x: x.pct_change(periods=periods,
3940+
fill_method=fill_method,
3941+
limit=limit, freq=freq))
39703942

39713943

39723944
class NDFrameGroupBy(GroupBy):

pandas/tests/groupby/test_transform.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -723,16 +723,13 @@ def interweave(list_obj):
723723

724724
@pytest.mark.parametrize("test_series", [True, False])
725725
@pytest.mark.parametrize("shuffle", [True, False])
726-
@pytest.mark.parametrize("activate_cache", [True, False])
727726
@pytest.mark.parametrize("periods,fill_method,limit", [
728727
(1, 'ffill', None), (1, 'ffill', 1),
729728
(1, 'bfill', None), (1, 'bfill', 1),
730729
(-1, 'ffill', None), (-1, 'ffill', 1),
731730
(-1, 'bfill', None), (-1, 'bfill', 1),
732-
(-1, None, None), (-1, None, 1),
733-
(-1, None, None), (-1, None, 1)
734731
])
735-
def test_pct_change(test_series, shuffle, activate_cache, periods, fill_method, limit):
732+
def test_pct_change(test_series, shuffle, periods, fill_method, limit):
736733
vals = [3, np.nan, 1, 2, 4, 10, np.nan, 9]
737734
keys = ['a', 'b']
738735
key_v = np.repeat(keys, len(vals))
@@ -751,9 +748,6 @@ def test_pct_change(test_series, shuffle, activate_cache, periods, fill_method,
751748
exp = pd.DataFrame(exp_vals.values, columns=['A'])
752749
grp = df.groupby('key')
753750

754-
if activate_cache:
755-
grp.grouper.is_monotonic
756-
757751
def get_result(grp_obj):
758752
return grp_obj.pct_change(periods=periods,
759753
fill_method=fill_method,

0 commit comments

Comments
 (0)