Skip to content

Commit d037e65

Browse files
committed
Update test to address WillAyds code suggestion
1 parent 77c3935 commit d037e65

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

pandas/core/groupby/groupby.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,19 +2076,16 @@ def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None,
20762076
limit=limit, freq=freq,
20772077
axis=axis))
20782078
if fill_method:
2079-
new = DataFrameGroupBy(self._obj_with_exclusions,
2080-
grouper=self.grouper)
2079+
new = copy.copy(self)
20812080
new.obj = getattr(new, fill_method)(limit=limit)
20822081
new._reset_cache()
20832082
else:
20842083
new = self
20852084

20862085
obj = new.obj.drop(self.grouper.names, axis=1)
2087-
shifted = new.shift(periods=periods, freq=freq).\
2088-
drop(self.grouper.names, axis=1)
2086+
shifted = new.shift(periods=periods, freq=freq)
20892087
return (obj / shifted) - 1
20902088

2091-
20922089
@Substitution(name='groupby')
20932090
@Appender(_doc_template)
20942091
def head(self, n=5):

pandas/tests/groupby/test_transform.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -724,35 +724,41 @@ def interweave(list_obj):
724724
@pytest.mark.parametrize("test_series", [True, False])
725725
@pytest.mark.parametrize("shuffle", [True, False])
726726
@pytest.mark.parametrize("periods,fill_method,limit", [
727+
(1, None, None), (1, None, 1),
727728
(1, 'ffill', None), (1, 'ffill', 1),
728729
(1, 'bfill', None), (1, 'bfill', 1),
729730
(-1, 'ffill', None), (-1, 'ffill', 1),
730731
(-1, 'bfill', None), (-1, 'bfill', 1),
731732
])
732733
def test_pct_change(test_series, shuffle, periods, fill_method, limit):
733-
vals = [3, np.nan, 1, 2, 4, 10, np.nan, 4]
734+
# GH 21200, 21621
735+
vals = [3, np.nan, np.nan, np.nan, 1, 2, 4, 10, np.nan, 4]
734736
keys = ['a', 'b']
735737
key_v = np.repeat(keys, len(vals))
736738
df = DataFrame({'key': key_v, 'vals': vals * 2})
737739
if shuffle:
738740
order = np.random.RandomState(seed=42).permutation(len(df))
739741
df = df.reindex(order).reset_index(drop=True)
740742

741-
df = getattr(df.groupby('key'), fill_method)(limit=limit)
742-
grp = df.groupby('key')
743-
exp = grp['vals'].shift(0) / grp['vals'].shift(periods) - 1
744-
exp = exp.to_frame('vals')
743+
if fill_method:
744+
df_g = getattr(df.groupby('key'), fill_method)(limit=limit)
745+
grp = df_g.groupby('key')
746+
else:
747+
grp = df.groupby('key')
748+
749+
expected = grp['vals'].obj / grp['vals'].shift(periods) - 1
750+
expected = expected.to_frame('vals')
745751

746752
if test_series:
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'])
753+
result = df.groupby('key')['vals'].pct_change(periods=periods,
754+
fill_method=fill_method,
755+
limit=limit)
756+
tm.assert_series_equal(result, expected.loc[:, 'vals'])
751757
else:
752-
result = grp.pct_change(periods=periods,
753-
fill_method=fill_method,
754-
limit=limit)
755-
tm.assert_frame_equal(result, exp)
758+
result = df.groupby('key').pct_change(periods=periods,
759+
fill_method=fill_method,
760+
limit=limit)
761+
tm.assert_frame_equal(result, expected)
756762

757763

758764
@pytest.mark.parametrize("func", [np.any, np.all])

0 commit comments

Comments
 (0)