-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Named aggregations with multiple columns #33306
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9285b6c
Finished implementation of aggregation feature
mason-98 cd037e0
Merge branch 'master' of github.com:CSCD01/pandas-team24 into issue-2…
fpunny 37522f0
Fixed attributeError catch
fpunny 96346f9
Updated documentation to reflect multi column named aggregation
fpunny ccbc403
Changes to documentation
fpunny 046e9cb
Fixed lint issue
fpunny 1919892
fixed zip
fpunny 6817d26
Add test cases to groupby aggregate
jadenyjw 35d9129
Fixed implementation
fpunny 22499b8
Merge branch 'issue-29268' of github.com:CSCD01/pandas-team24 into is…
fpunny 7016e67
Run linter
fpunny 883a601
Added whatsnew entry
fpunny f599eb5
changed missing key test
2b08514
fixed failing pip8 error
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,6 +299,7 @@ def _aggregate(self, arg, *args, **kwargs): | |
None if not required | ||
""" | ||
is_aggregator = lambda x: isinstance(x, (list, tuple, dict)) | ||
deserialized_keys = {} | ||
|
||
_axis = kwargs.pop("_axis", None) | ||
if _axis is None: | ||
|
@@ -339,8 +340,22 @@ def _aggregate(self, arg, *args, **kwargs): | |
raise SpecificationError("nested renamer is not supported") | ||
elif isinstance(obj, ABCSeries): | ||
raise SpecificationError("nested renamer is not supported") | ||
elif isinstance(obj, ABCDataFrame) and k not in obj.columns: | ||
raise KeyError(f"Column '{k}' does not exist!") | ||
elif isinstance(obj, ABCDataFrame): | ||
# GH 29268 | ||
if k not in obj.columns: | ||
# Check if list thingy | ||
try: | ||
keys = np.frombuffer(k, dtype=np.dtype("<U1")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't want to do things like this, use is_list_like |
||
except (AttributeError, TypeError): | ||
raise KeyError(f"Column '{k}' does not exist!") | ||
|
||
# Check keys | ||
for key in keys: | ||
if key not in obj.columns: | ||
raise KeyError(f"Column '{key}' does not exist!") | ||
|
||
# Memorize operation | ||
deserialized_keys[k] = keys | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we keeping state? |
||
|
||
arg = new_arg | ||
|
||
|
@@ -374,14 +389,28 @@ def _agg_2dim(how): | |
colg = self._gotitem(self._selection, ndim=2, subset=obj) | ||
return colg.aggregate(how) | ||
|
||
# GH 29268 | ||
def _agg_multi_dim(name, how, keys): | ||
from pandas.core.frame import DataFrame | ||
|
||
_obj = {k: self._gotitem(k, ndim=1, subset=None) for k in keys} | ||
result = {com.get_callable_name(agg): agg(_obj) for agg in how} | ||
return DataFrame(result, columns=result.keys()) | ||
|
||
def _agg(arg, func): | ||
""" | ||
run the aggregations over the arg with func | ||
return a dict | ||
""" | ||
result = {} | ||
for fname, agg_how in arg.items(): | ||
result[fname] = func(fname, agg_how) | ||
# GH 29268 | ||
if fname in deserialized_keys: | ||
keys = deserialized_keys[fname] | ||
result[fname] = _agg_multi_dim(fname, agg_how, keys) | ||
else: | ||
result[fname] = func(fname, agg_how) | ||
|
||
return result | ||
|
||
# set the final keys | ||
|
@@ -412,11 +441,9 @@ def _agg(arg, func): | |
|
||
# no selection | ||
else: | ||
|
||
try: | ||
result = _agg(arg, _agg_1dim) | ||
except SpecificationError: | ||
|
||
# we are aggregating expecting all 1d-returns | ||
# but we have 2d | ||
result = _agg(arg, _agg_2dim) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you do a pre-cursor PR to move the current code to pandas/core/aggregation.py (so that you just call a function here). this is too complicated