Skip to content

BUG: Remove error raise to allow same input function on the same column in named aggregation #28428

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 36 commits into from
Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7e461a1
remove \n from docstring
charlesdong1991 Dec 3, 2018
1314059
fix conflicts
charlesdong1991 Jan 19, 2019
8bcb313
Merge remote-tracking branch 'upstream/master'
charlesdong1991 Jul 30, 2019
07c6003
Merge remote-tracking branch 'upstream/master' into fix_issue_28426
charlesdong1991 Sep 13, 2019
495f4ca
Remove error raising
charlesdong1991 Sep 13, 2019
65f3a34
better test case
charlesdong1991 Sep 13, 2019
b985055
Fix test for series
charlesdong1991 Sep 13, 2019
fb43351
Add whatsnew
charlesdong1991 Sep 13, 2019
2bc05c3
better english
charlesdong1991 Sep 13, 2019
39d3aee
more accurate explanation
charlesdong1991 Sep 13, 2019
df51bf9
Remove unused imports
charlesdong1991 Sep 13, 2019
d162b3e
code change based on review
charlesdong1991 Sep 14, 2019
31e8079
fix test
charlesdong1991 Sep 14, 2019
7e87435
code change and reformat
charlesdong1991 Sep 15, 2019
1361236
move whatsnew to 1.0.0
charlesdong1991 Sep 16, 2019
3815507
add test
charlesdong1991 Sep 17, 2019
8a9c41b
bold change
charlesdong1991 Sep 17, 2019
6eeb978
remove unused comment
charlesdong1991 Sep 17, 2019
2ae946e
remove print
charlesdong1991 Sep 18, 2019
2e5fd6e
revert blank line change
charlesdong1991 Sep 18, 2019
b10b4b8
revert previous change
charlesdong1991 Sep 22, 2019
314eb28
revert unnecessary white space removal
charlesdong1991 Sep 22, 2019
3fc2d71
fix conflicts and merge master
charlesdong1991 Oct 19, 2019
fd485d7
fix tests
charlesdong1991 Oct 19, 2019
5817f5e
fix test
charlesdong1991 Oct 19, 2019
84f2734
fix conflict and repush
charlesdong1991 Nov 8, 2019
0e249fc
move to groupby section
charlesdong1991 Nov 8, 2019
6331a26
Merge remote-tracking branch 'upstream/master' into fix_issue_28426
charlesdong1991 Nov 8, 2019
5e1e4e0
merge master
charlesdong1991 Nov 20, 2019
f7a219c
fix test for multiindex
charlesdong1991 Nov 20, 2019
cef6d3a
reformatting
charlesdong1991 Nov 20, 2019
2c11c36
solve conflicts
charlesdong1991 Nov 22, 2019
5172f46
solve conflict
charlesdong1991 Nov 22, 2019
6c7ee71
merge master and fix conflict
charlesdong1991 Dec 2, 2019
90ba1b4
remove redundant and expand whatsnew note
charlesdong1991 Dec 2, 2019
8eaecee
Merge remote-tracking branch 'upstream/master' into fix_issue_28426
charlesdong1991 Dec 3, 2019
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
Prev Previous commit
Next Next commit
code change based on review
  • Loading branch information
charlesdong1991 committed Sep 14, 2019
commit d162b3e06e77bb2f5656512f99101e7c99f024e5
15 changes: 14 additions & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

from pandas._typing import FrameOrSeries
import pandas.core.algorithms as algorithms
from pandas.core.base import DataError
from pandas.core.base import DataError, SpecificationError
import pandas.core.common as com
from pandas.core.frame import DataFrame
from pandas.core.generic import ABCDataFrame, ABCSeries, NDFrame, _shared_docs
Expand Down Expand Up @@ -230,6 +230,13 @@ def aggregate(self, func, *args, **kwargs):
elif func is None:
# nicer error message
raise TypeError("Must provide 'func' or tuples of '(column, aggfunc).")
elif isinstance(func, list) and len(func) > len(set(func)):

# GH 28426 will raise error if duplicated function names are used and
raise SpecificationError(
"Function names must be unique if there is no new column "
"names assigned"
)

func = _maybe_mangle_lambdas(func)

Expand Down Expand Up @@ -857,6 +864,12 @@ def aggregate(self, func=None, *args, **kwargs):
kwargs = {}
if not columns:
raise TypeError(no_arg_message)
elif isinstance(func, list) and len(func) > len(set(func)):
# GH 28426 will raise error if duplicated function names are used and
raise SpecificationError(
"Function names must be unique if there is no new column "
"names assigned"
)

if isinstance(func, str):
return getattr(self, func)(*args, **kwargs)
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pandas as pd
from pandas import DataFrame, Index, MultiIndex, Series, compat, concat
from pandas.core.base import SpecificationError
from pandas.core.groupby.generic import _make_unique, _maybe_mangle_lambdas
from pandas.core.groupby.grouper import Grouping
import pandas.util.testing as tm
Expand Down Expand Up @@ -349,6 +350,18 @@ def test_uint64_type_handling(dtype, how):
tm.assert_frame_equal(result, expected, check_exact=True)


def test_func_duplicates_raises():
# GH28426
gr = pd.Series([1, 2, 3]).groupby([0, 0, 1])
msg = "Function names"
with pytest.raises(SpecificationError, match=msg):
gr.agg(['sum', 'sum'])

df = pd.DataFrame({"A": [0, 0, 1, 1], "B": [1, 2, 3, 4]})
with pytest.raises(SpecificationError, match=msg):
df.groupby("A").agg(['min', 'min'])


class TestNamedAggregationSeries:
def test_series_named_agg(self):
df = pd.Series([1, 2, 3, 4])
Expand Down