Skip to content

Commit f525864

Browse files
authored
REF: de-duplicate operate-column-wise code, pct_change (pandas-dev#43506)
1 parent 9030685 commit f525864

File tree

2 files changed

+6
-33
lines changed

2 files changed

+6
-33
lines changed

pandas/core/groupby/generic.py

+4-33
Original file line numberDiff line numberDiff line change
@@ -792,24 +792,6 @@ def count(self) -> Series:
792792
)
793793
return self._reindex_output(result, fill_value=0)
794794

795-
def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None):
796-
"""Calculate pct_change of each value to previous entry in group"""
797-
# TODO: Remove this conditional when #23918 is fixed
798-
if freq:
799-
return self.apply(
800-
lambda x: x.pct_change(
801-
periods=periods, fill_method=fill_method, limit=limit, freq=freq
802-
)
803-
)
804-
if fill_method is None: # GH30463
805-
fill_method = "pad"
806-
limit = 0
807-
filled = getattr(self, fill_method)(limit=limit)
808-
fill_grp = filled.groupby(self.grouper.codes)
809-
shifted = fill_grp.shift(periods=periods, freq=freq)
810-
811-
return (filled / shifted) - 1
812-
813795
@doc(Series.nlargest)
814796
def nlargest(self, n: int = 5, keep: str = "first"):
815797
f = partial(Series.nlargest, n=n, keep=keep)
@@ -1086,14 +1068,10 @@ def _aggregate_item_by_item(self, func, *args, **kwargs) -> DataFrame:
10861068
# test_resample_apply_product
10871069

10881070
obj = self._obj_with_exclusions
1089-
result: dict[int | str, NDFrame] = {}
1090-
for i, item in enumerate(obj):
1091-
ser = obj.iloc[:, i]
1092-
colg = SeriesGroupBy(
1093-
ser, selection=item, grouper=self.grouper, exclusions=self.exclusions
1094-
)
1071+
result: dict[int, NDFrame] = {}
10951072

1096-
result[i] = colg.aggregate(func, *args, **kwargs)
1073+
for i, (item, sgb) in enumerate(self._iterate_column_groupbys(obj)):
1074+
result[i] = sgb.aggregate(func, *args, **kwargs)
10971075

10981076
res_df = self.obj._constructor(result)
10991077
res_df.columns = obj.columns
@@ -1368,14 +1346,7 @@ def _transform_item_by_item(self, obj: DataFrame, wrapper) -> DataFrame:
13681346
# gets here with non-unique columns
13691347
output = {}
13701348
inds = []
1371-
for i, col in enumerate(obj):
1372-
subset = obj.iloc[:, i]
1373-
sgb = SeriesGroupBy(
1374-
subset,
1375-
selection=col,
1376-
grouper=self.grouper,
1377-
exclusions=self.exclusions,
1378-
)
1349+
for i, (colname, sgb) in enumerate(self._iterate_column_groupbys(obj)):
13791350
try:
13801351
output[i] = sgb.transform(wrapper)
13811352
except TypeError:

pandas/core/groupby/groupby.py

+2
Original file line numberDiff line numberDiff line change
@@ -3236,6 +3236,7 @@ def shift(self, periods=1, freq=None, axis=0, fill_value=None):
32363236
)
32373237
return res
32383238

3239+
@final
32393240
@Substitution(name="groupby")
32403241
@Appender(_common_see_also)
32413242
def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None, axis=0):
@@ -3247,6 +3248,7 @@ def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None, axis=0
32473248
Series or DataFrame
32483249
Percentage changes within each group.
32493250
"""
3251+
# TODO: Remove this conditional for SeriesGroupBy when GH#23918 is fixed
32503252
if freq is not None or axis != 0:
32513253
return self.apply(
32523254
lambda x: x.pct_change(

0 commit comments

Comments
 (0)