Skip to content

Commit b6fceec

Browse files
committed
CLN: paramters for GroupBy.(mean|median|var|std)
1 parent d81e226 commit b6fceec

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Backwards incompatible API changes
6060
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6161
- :meth:`DataFrame.swaplevels` now raises a ``TypeError`` if the axis is not a :class:`MultiIndex`.
6262
Previously a ``AttributeError`` was raised (:issue:`31126`)
63+
- :meth:`DataFrameGroupby.mean` and :meth:`SeriesGroupby.mean` (and similarly for ``median``, ``std`` and ``var``)
64+
now raises a ``TypeError`` if a not-accepted keyword argument is passed into it. Previously a ``UnsupportedFunctionCall`` was raised. (:issue:`31485`)
6365

6466

6567
.. ---------------------------------------------------------------------------

pandas/core/groupby/groupby.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ def count(self):
11801180

11811181
@Substitution(name="groupby")
11821182
@Substitution(see_also=_common_see_also)
1183-
def mean(self, *args, **kwargs):
1183+
def mean(self, numeric_only: bool = True):
11841184
"""
11851185
Compute mean of groups, excluding missing values.
11861186
@@ -1222,14 +1222,15 @@ def mean(self, *args, **kwargs):
12221222
2 4.0
12231223
Name: B, dtype: float64
12241224
"""
1225-
nv.validate_groupby_func("mean", args, kwargs, ["numeric_only"])
12261225
return self._cython_agg_general(
1227-
"mean", alt=lambda x, axis: Series(x).mean(**kwargs), **kwargs
1226+
"mean",
1227+
alt=lambda x, axis: Series(x).mean(numeric_only=numeric_only),
1228+
numeric_only=numeric_only,
12281229
)
12291230

12301231
@Substitution(name="groupby")
12311232
@Appender(_common_see_also)
1232-
def median(self, **kwargs):
1233+
def median(self, numeric_only=True, min_count=-1):
12331234
"""
12341235
Compute median of groups, excluding missing values.
12351236
@@ -1242,13 +1243,16 @@ def median(self, **kwargs):
12421243
"""
12431244
return self._cython_agg_general(
12441245
"median",
1245-
alt=lambda x, axis: Series(x).median(axis=axis, **kwargs),
1246-
**kwargs,
1246+
alt=lambda x, axis: Series(x).median(
1247+
axis=axis, numeric_only=numeric_only, min_count=numeric_only
1248+
),
1249+
numeric_only=numeric_only,
1250+
min_count=min_count,
12471251
)
12481252

12491253
@Substitution(name="groupby")
12501254
@Appender(_common_see_also)
1251-
def std(self, ddof: int = 1, *args, **kwargs):
1255+
def std(self, ddof: int = 1):
12521256
"""
12531257
Compute standard deviation of groups, excluding missing values.
12541258
@@ -1266,12 +1270,11 @@ def std(self, ddof: int = 1, *args, **kwargs):
12661270
"""
12671271

12681272
# TODO: implement at Cython level?
1269-
nv.validate_groupby_func("std", args, kwargs)
1270-
return np.sqrt(self.var(ddof=ddof, **kwargs))
1273+
return np.sqrt(self.var(ddof=ddof))
12711274

12721275
@Substitution(name="groupby")
12731276
@Appender(_common_see_also)
1274-
def var(self, ddof: int = 1, *args, **kwargs):
1277+
def var(self, ddof: int = 1):
12751278
"""
12761279
Compute variance of groups, excluding missing values.
12771280
@@ -1287,15 +1290,14 @@ def var(self, ddof: int = 1, *args, **kwargs):
12871290
Series or DataFrame
12881291
Variance of values within each group.
12891292
"""
1290-
nv.validate_groupby_func("var", args, kwargs)
12911293
if ddof == 1:
12921294
return self._cython_agg_general(
1293-
"var", alt=lambda x, axis: Series(x).var(ddof=ddof, **kwargs), **kwargs
1295+
"var", alt=lambda x, axis: Series(x).var(ddof=ddof)
12941296
)
12951297
else:
1296-
f = lambda x: x.var(ddof=ddof, **kwargs)
1298+
func = lambda x: x.var(ddof=ddof)
12971299
with _group_selection_context(self):
1298-
return self._python_agg_general(f)
1300+
return self._python_agg_general(func)
12991301

13001302
@Substitution(name="groupby")
13011303
@Appender(_common_see_also)

pandas/tests/groupby/test_function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ def test_nsmallest():
671671
tm.assert_series_equal(gb.nsmallest(3, keep="last"), e)
672672

673673

674-
@pytest.mark.parametrize("func", ["mean", "var", "std", "cumprod", "cumsum"])
674+
@pytest.mark.parametrize("func", ["cumprod", "cumsum"])
675675
def test_numpy_compat(func):
676676
# see gh-12811
677677
df = pd.DataFrame({"A": [1, 2, 1], "B": [1, 2, 3]})

0 commit comments

Comments
 (0)