Skip to content

Commit aa134bb

Browse files
maushumeemroeschke
andauthored
BUG Fix for Add numeric_only to function signature of DataFrameGroupBy.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 <[email protected]> * Update pandas/core/groupby/groupby.py Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent ac69522 commit aa134bb

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ Groupby/resample/rolling
610610
- 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`)
611611
- 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`)
612612
- Bug in :meth:`DataFrameGroupBy.apply` with ``as_index=False`` that was returning :class:`MultiIndex` instead of returning :class:`Index`. (:issue:`58291`)
613+
- Bug in :meth:`DataFrameGroupBy.cumsum` and :meth:`DataFrameGroupBy.cumprod` where ``numeric_only`` parameter was passed indirectly through kwargs instead of passing directly. (:issue:`58811`)
613614
- Bug in :meth:`DataFrameGroupBy.cumsum` where it did not return the correct dtype when the label contained ``None``. (:issue:`58811`)
614615
- 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`)
615616
- Bug in :meth:`Rolling.apply` where the applied function could be called on fewer than ``min_period`` periods if ``method="table"``. (:issue:`58868`)

pandas/core/groupby/groupby.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -4682,12 +4682,14 @@ def rank(
46824682
@final
46834683
@Substitution(name="groupby")
46844684
@Substitution(see_also=_common_see_also)
4685-
def cumprod(self, *args, **kwargs) -> NDFrameT:
4685+
def cumprod(self, numeric_only: bool = False, *args, **kwargs) -> NDFrameT:
46864686
"""
46874687
Cumulative product for each group.
46884688
46894689
Parameters
46904690
----------
4691+
numeric_only : bool, default False
4692+
Include only float, int, boolean columns.
46914693
*args : tuple
46924694
Positional arguments to be passed to `func`.
46934695
**kwargs : dict
@@ -4735,18 +4737,20 @@ def cumprod(self, *args, **kwargs) -> NDFrameT:
47354737
horse 16 10
47364738
bull 6 9
47374739
"""
4738-
nv.validate_groupby_func("cumprod", args, kwargs, ["numeric_only", "skipna"])
4739-
return self._cython_transform("cumprod", **kwargs)
4740+
nv.validate_groupby_func("cumprod", args, kwargs, ["skipna"])
4741+
return self._cython_transform("cumprod", numeric_only, **kwargs)
47404742

47414743
@final
47424744
@Substitution(name="groupby")
47434745
@Substitution(see_also=_common_see_also)
4744-
def cumsum(self, *args, **kwargs) -> NDFrameT:
4746+
def cumsum(self, numeric_only: bool = False, *args, **kwargs) -> NDFrameT:
47454747
"""
47464748
Cumulative sum for each group.
47474749
47484750
Parameters
47494751
----------
4752+
numeric_only : bool, default False
4753+
Include only float, int, boolean columns.
47504754
*args : tuple
47514755
Positional arguments to be passed to `func`.
47524756
**kwargs : dict
@@ -4794,8 +4798,8 @@ def cumsum(self, *args, **kwargs) -> NDFrameT:
47944798
gorilla 10 7
47954799
lion 6 9
47964800
"""
4797-
nv.validate_groupby_func("cumsum", args, kwargs, ["numeric_only", "skipna"])
4798-
return self._cython_transform("cumsum", **kwargs)
4801+
nv.validate_groupby_func("cumsum", args, kwargs, ["skipna"])
4802+
return self._cython_transform("cumsum", numeric_only, **kwargs)
47994803

48004804
@final
48014805
@Substitution(name="groupby")

pandas/tests/groupby/test_api.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def test_frame_consistency(groupby_func):
185185
elif groupby_func in ("cummax", "cummin"):
186186
exclude_expected = {"axis", "skipna", "args"}
187187
elif groupby_func in ("cumprod", "cumsum"):
188-
exclude_expected = {"axis", "skipna", "numeric_only"}
188+
exclude_expected = {"axis", "skipna"}
189189
elif groupby_func in ("pct_change",):
190190
exclude_expected = {"kwargs"}
191191
elif groupby_func in ("rank",):
@@ -245,6 +245,7 @@ def test_series_consistency(request, groupby_func):
245245
exclude_result = {"numeric_only"}
246246
elif groupby_func in ("cumprod", "cumsum"):
247247
exclude_expected = {"skipna"}
248+
exclude_result = {"numeric_only"}
248249
elif groupby_func in ("pct_change",):
249250
exclude_expected = {"kwargs"}
250251
elif groupby_func in ("rank",):

0 commit comments

Comments
 (0)