Skip to content

Commit 77c3935

Browse files
committed
BUG,TST: Remove case where vectorization fails in pct_change groupby method. Incorporate CR suggestion.
1 parent e8681d6 commit 77c3935

File tree

1 file changed

+10
-27
lines changed

1 file changed

+10
-27
lines changed

pandas/tests/groupby/test_transform.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -738,37 +738,20 @@ def test_pct_change(test_series, shuffle, periods, fill_method, limit):
738738
order = np.random.RandomState(seed=42).permutation(len(df))
739739
df = df.reindex(order).reset_index(drop=True)
740740

741-
manual_apply = []
742-
for k in keys:
743-
ind = df.loc[df.key == k, 'vals']
744-
manual_apply.append(ind.pct_change(periods=periods,
745-
fill_method=fill_method,
746-
limit=limit))
747-
exp_vals = pd.concat(manual_apply, ignore_index=True)
748-
exp = pd.DataFrame(exp_vals.values, columns=['A'])
741+
df = getattr(df.groupby('key'), fill_method)(limit=limit)
749742
grp = df.groupby('key')
750-
751-
def get_result(grp_obj):
752-
return grp_obj.pct_change(periods=periods,
753-
fill_method=fill_method,
754-
limit=limit)
743+
exp = grp['vals'].shift(0) / grp['vals'].shift(periods) - 1
744+
exp = exp.to_frame('vals')
755745

756746
if test_series:
757-
exp = exp.loc[:, 'A']
758-
grp = grp['vals']
759-
result = get_result(grp)
760-
df.insert(0, 'A', result)
761-
result = df.sort_values(by='key')
762-
result = result.loc[:, 'A']
763-
result = result.reset_index(drop=True)
764-
tm.assert_series_equal(result, exp)
747+
result = grp['vals'].pct_change(periods=periods,
748+
fill_method=fill_method,
749+
limit=limit)
750+
tm.assert_series_equal(result, exp.loc[:, 'vals'])
765751
else:
766-
result = get_result(grp)
767-
result = result.reset_index(drop=True)
768-
df.insert(0, 'A', result)
769-
result = df.sort_values(by='key')
770-
result = result.loc[:, ['A']]
771-
result = result.reset_index(drop=True)
752+
result = grp.pct_change(periods=periods,
753+
fill_method=fill_method,
754+
limit=limit)
772755
tm.assert_frame_equal(result, exp)
773756

774757

0 commit comments

Comments
 (0)