Skip to content

API: Remove kwargs from GroupBy #29511

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 3 commits into from
Nov 12, 2019
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
11 changes: 3 additions & 8 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7830,7 +7830,6 @@ def groupby(
group_keys=True,
squeeze=False,
observed=False,
**kwargs
):
"""
Group DataFrame or Series using a mapper or by a Series of columns.
Expand Down Expand Up @@ -7876,10 +7875,6 @@ def groupby(

.. versionadded:: 0.23.0

**kwargs
Optional, only accepts keyword argument 'mutated' and is passed
to groupby.

Returns
-------
DataFrameGroupBy or SeriesGroupBy
Expand Down Expand Up @@ -7941,12 +7936,13 @@ def groupby(
Captive 210.0
Wild 185.0
"""
from pandas.core.groupby.groupby import groupby
from pandas.core.groupby.groupby import get_groupby

if level is None and by is None:
raise TypeError("You have to supply one of 'by' and 'level'")
axis = self._get_axis_number(axis)
return groupby(

return get_groupby(
self,
by=by,
axis=axis,
Expand All @@ -7956,7 +7952,6 @@ def groupby(
group_keys=group_keys,
squeeze=squeeze,
observed=observed,
**kwargs
)

def asfreq(self, freq, method=None, how=None, normalize=False, fill_value=None):
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
GroupBy,
_apply_docs,
_transform_template,
groupby,
get_groupby,
)
from pandas.core.index import Index, MultiIndex, _all_indexes_same
import pandas.core.indexes.base as ibase
Expand Down Expand Up @@ -997,7 +997,7 @@ def _cython_agg_blocks(
# reductions; see GH#28949
obj = obj.iloc[:, 0]

s = groupby(obj, self.grouper)
s = get_groupby(obj, self.grouper)
try:
result = s.aggregate(lambda x: alt(x, axis=self.axis))
except TypeError:
Expand Down
51 changes: 38 additions & 13 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class providing the base-class of operations.
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
from pandas.util._decorators import Appender, Substitution, cache_readonly
from pandas.util._validators import validate_kwargs

from pandas.core.dtypes.cast import maybe_downcast_to_dtype
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -349,12 +348,12 @@ def __init__(
grouper=None,
exclusions=None,
selection=None,
as_index=True,
sort=True,
group_keys=True,
squeeze=False,
observed=False,
**kwargs
as_index: bool = True,
sort: bool = True,
group_keys: bool = True,
squeeze: bool = False,
observed: bool = False,
mutated: bool = False,
):

self._selection = selection
Expand All @@ -376,7 +375,7 @@ def __init__(
self.group_keys = group_keys
self.squeeze = squeeze
self.observed = observed
self.mutated = kwargs.pop("mutated", False)
self.mutated = mutated

if grouper is None:
from pandas.core.groupby.grouper import get_grouper
Expand All @@ -396,9 +395,6 @@ def __init__(
self.grouper = grouper
self.exclusions = set(exclusions) if exclusions else set()

# we accept no other args
validate_kwargs("group", kwargs, {})

def __len__(self) -> int:
return len(self.groups)

Expand Down Expand Up @@ -2490,7 +2486,22 @@ def _reindex_output(self, output):


@Appender(GroupBy.__doc__)
def groupby(obj: NDFrame, by, **kwds):
def get_groupby(
obj: NDFrame,
by=None,
axis: int = 0,
level=None,
grouper=None,
exclusions=None,
selection=None,
as_index: bool = True,
sort: bool = True,
group_keys: bool = True,
squeeze: bool = False,
observed: bool = False,
mutated: bool = False,
):

if isinstance(obj, Series):
from pandas.core.groupby.generic import SeriesGroupBy

Expand All @@ -2504,4 +2515,18 @@ def groupby(obj: NDFrame, by, **kwds):
else:
raise TypeError("invalid type: {obj}".format(obj=obj))

return klass(obj, by, **kwds)
return klass(
obj=obj,
keys=by,
axis=axis,
level=level,
grouper=grouper,
exclusions=exclusions,
selection=selection,
as_index=as_index,
sort=sort,
group_keys=group_keys,
squeeze=squeeze,
observed=observed,
mutated=mutated,
)
6 changes: 3 additions & 3 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from pandas.core.generic import _shared_docs
from pandas.core.groupby.base import GroupByMixin
from pandas.core.groupby.generic import SeriesGroupBy
from pandas.core.groupby.groupby import GroupBy, _GroupBy, _pipe_template, groupby
from pandas.core.groupby.groupby import GroupBy, _GroupBy, _pipe_template, get_groupby
from pandas.core.groupby.grouper import Grouper
from pandas.core.groupby.ops import BinGrouper
from pandas.core.indexes.datetimes import DatetimeIndex, date_range
Expand Down Expand Up @@ -334,7 +334,7 @@ def _gotitem(self, key, ndim, subset=None):
grouper = self.grouper
if subset is None:
subset = self.obj
grouped = groupby(subset, by=None, grouper=grouper, axis=self.axis)
grouped = get_groupby(subset, by=None, grouper=grouper, axis=self.axis)

# try the key selection
try:
Expand All @@ -353,7 +353,7 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):

obj = self._selected_obj

grouped = groupby(obj, by=None, grouper=grouper, axis=self.axis)
grouped = get_groupby(obj, by=None, grouper=grouper, axis=self.axis)

try:
if isinstance(obj, ABCDataFrame) and callable(how):
Expand Down
9 changes: 5 additions & 4 deletions pandas/tests/window/test_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pandas as pd
from pandas import DataFrame, Series
from pandas.core.groupby.groupby import get_groupby
import pandas.util.testing as tm


Expand All @@ -13,18 +14,18 @@ def setup_method(self, method):

def test_mutated(self):

msg = r"group\(\) got an unexpected keyword argument 'foo'"
msg = r"groupby\(\) got an unexpected keyword argument 'foo'"
with pytest.raises(TypeError, match=msg):
self.frame.groupby("A", foo=1)

g = self.frame.groupby("A")
assert not g.mutated
g = self.frame.groupby("A", mutated=True)
g = get_groupby(self.frame, by="A", mutated=True)
assert g.mutated

def test_getitem(self):
g = self.frame.groupby("A")
g_mutated = self.frame.groupby("A", mutated=True)
g_mutated = get_groupby(self.frame, by="A", mutated=True)

expected = g_mutated.B.apply(lambda x: x.rolling(2).mean())

Expand All @@ -45,7 +46,7 @@ def test_getitem_multiple(self):
# GH 13174
g = self.frame.groupby("A")
r = g.rolling(2)
g_mutated = self.frame.groupby("A", mutated=True)
g_mutated = get_groupby(self.frame, by="A", mutated=True)
expected = g_mutated.B.apply(lambda x: x.rolling(2).count())

result = r.B.count()
Expand Down