Skip to content

REF: de-duplicate operate-column-wise code, pct_change #43506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 4 additions & 33 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,24 +791,6 @@ def count(self) -> Series:
)
return self._reindex_output(result, fill_value=0)

def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None):
"""Calculate pct_change of each value to previous entry in group"""
# TODO: Remove this conditional when #23918 is fixed
if freq:
return self.apply(
lambda x: x.pct_change(
periods=periods, fill_method=fill_method, limit=limit, freq=freq
)
)
if fill_method is None: # GH30463
fill_method = "pad"
limit = 0
filled = getattr(self, fill_method)(limit=limit)
fill_grp = filled.groupby(self.grouper.codes)
shifted = fill_grp.shift(periods=periods, freq=freq)

return (filled / shifted) - 1

@doc(Series.nlargest)
def nlargest(self, n: int = 5, keep: str = "first"):
f = partial(Series.nlargest, n=n, keep=keep)
Expand Down Expand Up @@ -1084,14 +1066,10 @@ def _aggregate_item_by_item(self, func, *args, **kwargs) -> DataFrame:
# test_resample_apply_product

obj = self._obj_with_exclusions
result: dict[int | str, NDFrame] = {}
for i, item in enumerate(obj):
ser = obj.iloc[:, i]
colg = SeriesGroupBy(
ser, selection=item, grouper=self.grouper, exclusions=self.exclusions
)
result: dict[int, NDFrame] = {}

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

res_df = self.obj._constructor(result)
res_df.columns = obj.columns
Expand Down Expand Up @@ -1370,14 +1348,7 @@ def _transform_item_by_item(self, obj: DataFrame, wrapper) -> DataFrame:
# gets here with non-unique columns
output = {}
inds = []
for i, col in enumerate(obj):
subset = obj.iloc[:, i]
sgb = SeriesGroupBy(
subset,
selection=col,
grouper=self.grouper,
exclusions=self.exclusions,
)
for i, (colname, sgb) in enumerate(self._iterate_column_groupbys(obj)):
try:
output[i] = sgb.transform(wrapper)
except TypeError:
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3210,6 +3210,7 @@ def shift(self, periods=1, freq=None, axis=0, fill_value=None):
)
return res

@final
@Substitution(name="groupby")
@Appender(_common_see_also)
def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None, axis=0):
Expand All @@ -3221,6 +3222,7 @@ def pct_change(self, periods=1, fill_method="pad", limit=None, freq=None, axis=0
Series or DataFrame
Percentage changes within each group.
"""
# TODO: Remove this conditional for SeriesGroupBy when GH#23918 is fixed
if freq is not None or axis != 0:
return self.apply(
lambda x: x.pct_change(
Expand Down