Skip to content

BUG: Fix to GH34422 SeriesGroupBy works only with 'func' now #34435

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 24 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
21bc1da
'TypeError is raised when tuple or list or NamedAgg is given in named…
gurukiran07 May 28, 2020
7233382
'BUG: #GH34422 raises TypeError when parameters to named aggregation …
gurukiran07 May 28, 2020
28c7403
'Fixed PEP-8 issues'
gurukiran07 May 28, 2020
a9ea9d1
'Fixed PEP-8 issues'
gurukiran07 May 28, 2020
b57f321
'Fixed PEP-8 issue'
gurukiran07 May 28, 2020
fa03577
'Added tests to test_aggregate.py'
gurukiran07 May 29, 2020
c7a7bdd
'Added tests in test_aggregate.py'
gurukiran07 May 29, 2020
85f1218
'Fixed bug'
gurukiran07 May 29, 2020
02b8139
'Fixed bug'
gurukiran07 May 29, 2020
0042811
'Fixed bug'
gurukiran07 May 29, 2020
e620b1e
'Added tests in test_aggregate.py'
gurukiran07 May 29, 2020
884e899
Added tests in test_aggregate.py
gurukiran07 May 29, 2020
b2ca7a9
Fixed Linting issues
gurukiran07 May 29, 2020
fe1451a
Fixed linting issues
gurukiran07 May 29, 2020
9a88d94
Added @pytest.mark.parametrize
gurukiran07 May 29, 2020
ce641c9
Added @pytest.mark.parameterize
gurukiran07 May 29, 2020
6356a92
Changed NamedAgg to tuple
gurukiran07 May 29, 2020
ea771f3
Added function to validate kwargs
gurukiran07 Jun 2, 2020
bb9a031
Fixed PEP-8 issues
gurukiran07 Jun 2, 2020
91b650f
Fixed PEP-8 isuues
gurukiran07 Jun 2, 2020
5406abf
Fixed docstring in validate_func_kwargs
gurukiran07 Jun 2, 2020
8a107e1
BUG: pandas SeriesGroupBy.agg issue 34422
gurukiran07 Jun 2, 2020
46f6ab1
BUG: SeriesGroupBy.agg allows any column name in named aggregation
gurukiran07 Jun 3, 2020
ffaf87b
Added f-string in test_aggregate.py and fixed v1.1.0.rst.
gurukiran07 Jun 3, 2020
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,7 @@ Groupby/resample/rolling
to the input DataFrame is inconsistent. An internal heuristic to detect index mutation would behave differently for equal but not identical
indices. In particular, the result index shape might change if a copy of the input would be returned.
The behaviour now is consistent, independent of internal heuristics. (:issue:`31612`, :issue:`14927`, :issue:`13056`)
- Bug in :meth:`SeriesGroupBy.agg` where any column name was accepted in the named aggregation of ``SeriesGroupBy`` previously. The behaviour now allows only ``str`` and callables else would raise ``TypeError``. (:issue:`34422`)

Reshaping
^^^^^^^^^
Expand Down
38 changes: 37 additions & 1 deletion pandas/core/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from collections import defaultdict
from functools import partial
from typing import Any, DefaultDict, List, Sequence, Tuple
from typing import Any, Callable, DefaultDict, List, Sequence, Tuple, Union

from pandas.core.dtypes.common import is_dict_like, is_list_like

Expand Down Expand Up @@ -196,3 +196,39 @@ def maybe_mangle_lambdas(agg_spec: Any) -> Any:
mangled_aggspec = _managle_lambda_list(agg_spec)

return mangled_aggspec


def validate_func_kwargs(
kwargs: dict,
) -> Tuple[List[str], List[Union[str, Callable[..., Any]]]]:
"""
Validates types of user-provided "named aggregation" kwargs.
`TypeError` is raised if aggfunc is not `str` or callable.

Parameters
----------
kwargs : dict

Returns
-------
columns : List[str]
List of user-provied keys.
func : List[Union[str, callable[...,Any]]]
List of user-provided aggfuncs

Examples
--------
>>> validate_func_kwargs({'one': 'min', 'two': 'max'})
(['one', 'two'], ['min', 'max'])
"""
no_arg_message = "Must provide 'func' or named aggregation **kwargs."
tuple_given_message = "func is expected but recieved {} in **kwargs."
columns = list(kwargs)
func = []
for col_func in kwargs.values():
if not (isinstance(col_func, str) or callable(col_func)):
raise TypeError(tuple_given_message.format(type(col_func).__name__))
func.append(col_func)
if not columns:
raise TypeError(no_arg_message)
return columns, func
7 changes: 2 additions & 5 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
is_multi_agg_with_relabel,
maybe_mangle_lambdas,
normalize_keyword_aggregation,
validate_func_kwargs,
)
import pandas.core.algorithms as algorithms
from pandas.core.base import DataError, SpecificationError
Expand Down Expand Up @@ -233,13 +234,9 @@ def aggregate(

relabeling = func is None
columns = None
no_arg_message = "Must provide 'func' or named aggregation **kwargs."
if relabeling:
columns = list(kwargs)
func = [kwargs[col] for col in columns]
columns, func = validate_func_kwargs(kwargs)
kwargs = {}
if not columns:
raise TypeError(no_arg_message)

if isinstance(func, str):
return getattr(self, func)(*args, **kwargs)
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,21 @@ def test_mangled(self):
expected = pd.DataFrame({"a": [0, 0], "b": [1, 1]})
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"inp",
[
pd.NamedAgg(column="anything", aggfunc="min"),
("anything", "min"),
["anything", "min"],
],
)
def test_named_agg_nametuple(self, inp):
# GH34422
s = pd.Series([1, 1, 2, 2, 3, 3, 4, 5])
msg = f"func is expected but recieved {type(inp).__name__}"
with pytest.raises(TypeError, match=msg):
s.groupby(s.values).agg(a=inp)


class TestNamedAggregationDataFrame:
def test_agg_relabel(self):
Expand Down