Skip to content

Commit

Permalink
BUG Fix for Add numeric_only to function signature of DataFrameGroupB…
Browse files Browse the repository at this point in the history
…y.cumprod and `DataFrameGroupBy.cumsum (#59427)

* Add numeric_only to func signature

* Add numeric_only to cumprod and cumsum func signature

* Added docstring

* Fix tests and add documentation

* Update pandas/core/groupby/groupby.py

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>

* Update pandas/core/groupby/groupby.py

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>

---------

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
  • Loading branch information
maushumee and mroeschke authored Aug 6, 2024
1 parent ac69522 commit aa134bb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ Groupby/resample/rolling
- Bug in :meth:`DataFrameGroupBy.agg` that raises ``AttributeError`` when there is dictionary input and duplicated columns, instead of returning a DataFrame with the aggregation of all duplicate columns. (:issue:`55041`)
- Bug in :meth:`DataFrameGroupBy.apply` that was returning a completely empty DataFrame when all return values of ``func`` were ``None`` instead of returning an empty DataFrame with the original columns and dtypes. (:issue:`57775`)
- Bug in :meth:`DataFrameGroupBy.apply` with ``as_index=False`` that was returning :class:`MultiIndex` instead of returning :class:`Index`. (:issue:`58291`)
- Bug in :meth:`DataFrameGroupBy.cumsum` and :meth:`DataFrameGroupBy.cumprod` where ``numeric_only`` parameter was passed indirectly through kwargs instead of passing directly. (:issue:`58811`)
- Bug in :meth:`DataFrameGroupBy.cumsum` where it did not return the correct dtype when the label contained ``None``. (:issue:`58811`)
- Bug in :meth:`DataFrameGroupby.transform` and :meth:`SeriesGroupby.transform` with a reducer and ``observed=False`` that coerces dtype to float when there are unobserved categories. (:issue:`55326`)
- Bug in :meth:`Rolling.apply` where the applied function could be called on fewer than ``min_period`` periods if ``method="table"``. (:issue:`58868`)
Expand Down
16 changes: 10 additions & 6 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -4682,12 +4682,14 @@ def rank(
@final
@Substitution(name="groupby")
@Substitution(see_also=_common_see_also)
def cumprod(self, *args, **kwargs) -> NDFrameT:
def cumprod(self, numeric_only: bool = False, *args, **kwargs) -> NDFrameT:
"""
Cumulative product for each group.
Parameters
----------
numeric_only : bool, default False
Include only float, int, boolean columns.
*args : tuple
Positional arguments to be passed to `func`.
**kwargs : dict
Expand Down Expand Up @@ -4735,18 +4737,20 @@ def cumprod(self, *args, **kwargs) -> NDFrameT:
horse 16 10
bull 6 9
"""
nv.validate_groupby_func("cumprod", args, kwargs, ["numeric_only", "skipna"])
return self._cython_transform("cumprod", **kwargs)
nv.validate_groupby_func("cumprod", args, kwargs, ["skipna"])
return self._cython_transform("cumprod", numeric_only, **kwargs)

@final
@Substitution(name="groupby")
@Substitution(see_also=_common_see_also)
def cumsum(self, *args, **kwargs) -> NDFrameT:
def cumsum(self, numeric_only: bool = False, *args, **kwargs) -> NDFrameT:
"""
Cumulative sum for each group.
Parameters
----------
numeric_only : bool, default False
Include only float, int, boolean columns.
*args : tuple
Positional arguments to be passed to `func`.
**kwargs : dict
Expand Down Expand Up @@ -4794,8 +4798,8 @@ def cumsum(self, *args, **kwargs) -> NDFrameT:
gorilla 10 7
lion 6 9
"""
nv.validate_groupby_func("cumsum", args, kwargs, ["numeric_only", "skipna"])
return self._cython_transform("cumsum", **kwargs)
nv.validate_groupby_func("cumsum", args, kwargs, ["skipna"])
return self._cython_transform("cumsum", numeric_only, **kwargs)

@final
@Substitution(name="groupby")
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/groupby/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_frame_consistency(groupby_func):
elif groupby_func in ("cummax", "cummin"):
exclude_expected = {"axis", "skipna", "args"}
elif groupby_func in ("cumprod", "cumsum"):
exclude_expected = {"axis", "skipna", "numeric_only"}
exclude_expected = {"axis", "skipna"}
elif groupby_func in ("pct_change",):
exclude_expected = {"kwargs"}
elif groupby_func in ("rank",):
Expand Down Expand Up @@ -245,6 +245,7 @@ def test_series_consistency(request, groupby_func):
exclude_result = {"numeric_only"}
elif groupby_func in ("cumprod", "cumsum"):
exclude_expected = {"skipna"}
exclude_result = {"numeric_only"}
elif groupby_func in ("pct_change",):
exclude_expected = {"kwargs"}
elif groupby_func in ("rank",):
Expand Down

0 comments on commit aa134bb

Please sign in to comment.