pandas icon indicating copy to clipboard operation
pandas copied to clipboard

BUG: Add ``numeric_only`` to function signature of ``DataFrameGroupBy.cumprod`` and ``DataFrameGroupBy.cumsum`

Open phofl opened this issue 2 years ago • 11 comments

Pandas version checks

  • [X] I have checked that this issue has not already been reported.

  • [X] I have confirmed this bug exists on the latest version of pandas.

  • [X] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

Both methods support numeric only but it goes through args or kwargs, which is annoying signature wise.

Issue Description

Add the keyword with the appropriate default

Expected Behavior

See above

Installed Versions

Replace this line with the output of pd.show_versions()

phofl avatar May 04 '23 13:05 phofl

take

ShashwatAgrawal20 avatar May 04 '23 14:05 ShashwatAgrawal20

@phofl something like?

    @final
    @Substitution(name="groupby")
    @Appender(_common_see_also)
    def cumprod(
        self, axis: Axis | lib.NoDefault = lib.no_default, numeric_only: bool = False, *args, **kwargs
    ) -> NDFrameT:
        """
        Cumulative product for each group.

        Returns
        -------
        Series or DataFrame
        """
        nv.validate_groupby_func("cumprod", args, kwargs, ["skipna"])
        if axis is not lib.no_default:
            axis = self.obj._get_axis_number(axis)
            self._deprecate_axis(axis, "cumprod")
        else:
            axis = 0

        if axis != 0:
            f = lambda x: x.cumprod(axis=axis, **kwargs)
            return self._python_apply_general(f, self._selected_obj, is_transform=True)

        return self._cython_transform("cumprod", numeric_only=numeric_only, **kwargs)

    @final
    @Substitution(name="groupby")
    @Appender(_common_see_also)
    def cumsum(
        self, axis: Axis | lib.NoDefault = lib.no_default, numeric_only: bool = False, *args, **kwargs
    ) -> NDFrameT:
        """
        Cumulative sum for each group.

        Returns
        -------
        Series or DataFrame
        """
        nv.validate_groupby_func("cumsum", args, kwargs, ["skipna"])
        if axis is not lib.no_default:
            axis = self.obj._get_axis_number(axis)
            self._deprecate_axis(axis, "cumsum")
        else:
            axis = 0

        if axis != 0:
            f = lambda x: x.cumsum(axis=axis, **kwargs)
            return self._python_apply_general(f, self._selected_obj, is_transform=True)

        return self._cython_transform("cumsum", numeric_only=numeric_only, **kwargs)

ShashwatAgrawal20 avatar May 04 '23 16:05 ShashwatAgrawal20

Yeah, we should probably add skipna as well

phofl avatar May 04 '23 18:05 phofl

Sure

ShashwatAgrawal20 avatar May 05 '23 04:05 ShashwatAgrawal20

I can look into this if it's still open @phofl @mroeschke ?

rsm-23 avatar Jun 27 '23 08:06 rsm-23

@ShashwatAgrawal20 are you working on this or can I take it up?

rsm-23 avatar Jul 01 '23 18:07 rsm-23

@ShashwatAgrawal20 are you working on this or can I take it up?

Go ahead

ShashwatAgrawal20 avatar Jul 01 '23 18:07 ShashwatAgrawal20

take

rsm-23 avatar Jul 01 '23 18:07 rsm-23

take

willhsp avatar Sep 11 '23 18:09 willhsp

@phofl I think the only way we can avoid making this a potentially breaking change is to use the following signature

def cumprod(
        self, 
        axis: Axis | lib.NoDefault = lib.no_default,
        *args,
        numeric_only: bool = False,
        skipna: bool = True,
        **kwargs
    ) -> NDFrameT:

This accounts for anyone passing in redundant positional/keyword arguments. The alternative would be to remove args and kwargs altogether. Is that something we'd be happy doing?

willhsp avatar Sep 14 '23 17:09 willhsp

If this issue is still open, I'd like to take it

shriyase avatar Jun 21 '24 20:06 shriyase

Hi @willhsp! Are you still working on this? Would love to contribute to this issue if you are not continuing. Thanks!

maushumee avatar Aug 05 '24 02:08 maushumee

take

maushumee avatar Aug 06 '24 01:08 maushumee