Skip to content

Commit 92b0546

Browse files
topper-123proost
authored andcommitted
API: Remove kwargs from GroupBy (pandas-dev#29511)
1 parent 4441ffa commit 92b0546

File tree

5 files changed

+51
-30
lines changed

5 files changed

+51
-30
lines changed

pandas/core/generic.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -7830,7 +7830,6 @@ def groupby(
78307830
group_keys=True,
78317831
squeeze=False,
78327832
observed=False,
7833-
**kwargs
78347833
):
78357834
"""
78367835
Group DataFrame or Series using a mapper or by a Series of columns.
@@ -7876,10 +7875,6 @@ def groupby(
78767875
78777876
.. versionadded:: 0.23.0
78787877
7879-
**kwargs
7880-
Optional, only accepts keyword argument 'mutated' and is passed
7881-
to groupby.
7882-
78837878
Returns
78847879
-------
78857880
DataFrameGroupBy or SeriesGroupBy
@@ -7941,12 +7936,13 @@ def groupby(
79417936
Captive 210.0
79427937
Wild 185.0
79437938
"""
7944-
from pandas.core.groupby.groupby import groupby
7939+
from pandas.core.groupby.groupby import get_groupby
79457940

79467941
if level is None and by is None:
79477942
raise TypeError("You have to supply one of 'by' and 'level'")
79487943
axis = self._get_axis_number(axis)
7949-
return groupby(
7944+
7945+
return get_groupby(
79507946
self,
79517947
by=by,
79527948
axis=axis,
@@ -7956,7 +7952,6 @@ def groupby(
79567952
group_keys=group_keys,
79577953
squeeze=squeeze,
79587954
observed=observed,
7959-
**kwargs
79607955
)
79617956

79627957
def asfreq(self, freq, method=None, how=None, normalize=False, fill_value=None):

pandas/core/groupby/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
GroupBy,
6363
_apply_docs,
6464
_transform_template,
65-
groupby,
65+
get_groupby,
6666
)
6767
from pandas.core.index import Index, MultiIndex, _all_indexes_same
6868
import pandas.core.indexes.base as ibase
@@ -996,7 +996,7 @@ def _cython_agg_blocks(
996996
# reductions; see GH#28949
997997
obj = obj.iloc[:, 0]
998998

999-
s = groupby(obj, self.grouper)
999+
s = get_groupby(obj, self.grouper)
10001000
try:
10011001
result = s.aggregate(lambda x: alt(x, axis=self.axis))
10021002
except TypeError:

pandas/core/groupby/groupby.py

+38-13
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class providing the base-class of operations.
2626
from pandas.compat.numpy import function as nv
2727
from pandas.errors import AbstractMethodError
2828
from pandas.util._decorators import Appender, Substitution, cache_readonly
29-
from pandas.util._validators import validate_kwargs
3029

3130
from pandas.core.dtypes.cast import maybe_downcast_to_dtype
3231
from pandas.core.dtypes.common import (
@@ -349,12 +348,12 @@ def __init__(
349348
grouper=None,
350349
exclusions=None,
351350
selection=None,
352-
as_index=True,
353-
sort=True,
354-
group_keys=True,
355-
squeeze=False,
356-
observed=False,
357-
**kwargs
351+
as_index: bool = True,
352+
sort: bool = True,
353+
group_keys: bool = True,
354+
squeeze: bool = False,
355+
observed: bool = False,
356+
mutated: bool = False,
358357
):
359358

360359
self._selection = selection
@@ -376,7 +375,7 @@ def __init__(
376375
self.group_keys = group_keys
377376
self.squeeze = squeeze
378377
self.observed = observed
379-
self.mutated = kwargs.pop("mutated", False)
378+
self.mutated = mutated
380379

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

399-
# we accept no other args
400-
validate_kwargs("group", kwargs, {})
401-
402398
def __len__(self) -> int:
403399
return len(self.groups)
404400

@@ -2482,7 +2478,22 @@ def _reindex_output(self, output):
24822478

24832479

24842480
@Appender(GroupBy.__doc__)
2485-
def groupby(obj: NDFrame, by, **kwds):
2481+
def get_groupby(
2482+
obj: NDFrame,
2483+
by=None,
2484+
axis: int = 0,
2485+
level=None,
2486+
grouper=None,
2487+
exclusions=None,
2488+
selection=None,
2489+
as_index: bool = True,
2490+
sort: bool = True,
2491+
group_keys: bool = True,
2492+
squeeze: bool = False,
2493+
observed: bool = False,
2494+
mutated: bool = False,
2495+
):
2496+
24862497
if isinstance(obj, Series):
24872498
from pandas.core.groupby.generic import SeriesGroupBy
24882499

@@ -2496,4 +2507,18 @@ def groupby(obj: NDFrame, by, **kwds):
24962507
else:
24972508
raise TypeError("invalid type: {obj}".format(obj=obj))
24982509

2499-
return klass(obj, by, **kwds)
2510+
return klass(
2511+
obj=obj,
2512+
keys=by,
2513+
axis=axis,
2514+
level=level,
2515+
grouper=grouper,
2516+
exclusions=exclusions,
2517+
selection=selection,
2518+
as_index=as_index,
2519+
sort=sort,
2520+
group_keys=group_keys,
2521+
squeeze=squeeze,
2522+
observed=observed,
2523+
mutated=mutated,
2524+
)

pandas/core/resample.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from pandas.core.generic import _shared_docs
2222
from pandas.core.groupby.base import GroupByMixin
2323
from pandas.core.groupby.generic import SeriesGroupBy
24-
from pandas.core.groupby.groupby import GroupBy, _GroupBy, _pipe_template, groupby
24+
from pandas.core.groupby.groupby import GroupBy, _GroupBy, _pipe_template, get_groupby
2525
from pandas.core.groupby.grouper import Grouper
2626
from pandas.core.groupby.ops import BinGrouper
2727
from pandas.core.indexes.datetimes import DatetimeIndex, date_range
@@ -334,7 +334,7 @@ def _gotitem(self, key, ndim, subset=None):
334334
grouper = self.grouper
335335
if subset is None:
336336
subset = self.obj
337-
grouped = groupby(subset, by=None, grouper=grouper, axis=self.axis)
337+
grouped = get_groupby(subset, by=None, grouper=grouper, axis=self.axis)
338338

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

354354
obj = self._selected_obj
355355

356-
grouped = groupby(obj, by=None, grouper=grouper, axis=self.axis)
356+
grouped = get_groupby(obj, by=None, grouper=grouper, axis=self.axis)
357357

358358
try:
359359
if isinstance(obj, ABCDataFrame) and callable(how):

pandas/tests/window/test_grouper.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pandas as pd
55
from pandas import DataFrame, Series
6+
from pandas.core.groupby.groupby import get_groupby
67
import pandas.util.testing as tm
78

89

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

1415
def test_mutated(self):
1516

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

2021
g = self.frame.groupby("A")
2122
assert not g.mutated
22-
g = self.frame.groupby("A", mutated=True)
23+
g = get_groupby(self.frame, by="A", mutated=True)
2324
assert g.mutated
2425

2526
def test_getitem(self):
2627
g = self.frame.groupby("A")
27-
g_mutated = self.frame.groupby("A", mutated=True)
28+
g_mutated = get_groupby(self.frame, by="A", mutated=True)
2829

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

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

5152
result = r.B.count()

0 commit comments

Comments
 (0)