-
-
Notifications
You must be signed in to change notification settings - Fork 19.5k
REF: Move generic methods to aggregation.py #30856
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
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7e461a1
remove \n from docstring
charlesdong1991 1314059
fix conflicts
charlesdong1991 8bcb313
Merge remote-tracking branch 'upstream/master'
charlesdong1991 b44519a
Merge remote-tracking branch 'upstream/master' into move_aggregate
charlesdong1991 ac5ea7d
move file to aggregation
charlesdong1991 3dd5dca
fix import
charlesdong1991 52947a4
add defaultdict
charlesdong1991 321ca76
Merge remote-tracking branch 'upstream/master' into move_aggregate
charlesdong1991 73855f3
code change based on JR review
charlesdong1991 3a97efe
move
charlesdong1991 91a9a7d
fixup
charlesdong1991 7e96879
fixup
charlesdong1991 a63278f
fix up
charlesdong1991 d2d2429
fix typing
charlesdong1991 6331408
better annotation
charlesdong1991 64a9991
fix black
charlesdong1991 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| """ | ||
| aggregation.py contains utility functions to handle multiple named and lambda | ||
| kwarg aggregations in groupby and DataFrame/Series aggregation | ||
| """ | ||
|
|
||
| from collections import defaultdict | ||
| from functools import partial | ||
| from typing import Any, DefaultDict, List, Sequence, Tuple | ||
|
|
||
| from pandas.core.dtypes.common import is_dict_like, is_list_like | ||
|
|
||
| import pandas.core.common as com | ||
| from pandas.core.indexes.api import Index | ||
|
|
||
|
|
||
| def is_multi_agg_with_relabel(**kwargs) -> bool: | ||
| """ | ||
| Check whether kwargs passed to .agg look like multi-agg with relabeling. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| **kwargs : dict | ||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> is_multi_agg_with_relabel(a='max') | ||
| False | ||
| >>> is_multi_agg_with_relabel(a_max=('a', 'max'), | ||
| ... a_min=('a', 'min')) | ||
| True | ||
| >>> is_multi_agg_with_relabel() | ||
| False | ||
| """ | ||
| return all(isinstance(v, tuple) and len(v) == 2 for v in kwargs.values()) and ( | ||
| len(kwargs) > 0 | ||
| ) | ||
|
|
||
|
|
||
| def normalize_keyword_aggregation(kwargs: dict) -> Tuple[dict, List[str], List[int]]: | ||
| """ | ||
| Normalize user-provided "named aggregation" kwargs. | ||
| Transforms from the new ``Mapping[str, NamedAgg]`` style kwargs | ||
| to the old Dict[str, List[scalar]]]. | ||
|
|
||
| Parameters | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ---------- | ||
| kwargs : dict | ||
|
|
||
| Returns | ||
| ------- | ||
| aggspec : dict | ||
| The transformed kwargs. | ||
| columns : List[str] | ||
| The user-provided keys. | ||
| col_idx_order : List[int] | ||
| List of columns indices. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> normalize_keyword_aggregation({'output': ('input', 'sum')}) | ||
| ({'input': ['sum']}, ('output',), [('input', 'sum')]) | ||
| """ | ||
| # Normalize the aggregation functions as Mapping[column, List[func]], | ||
| # process normally, then fixup the names. | ||
| # TODO: aggspec type: typing.Dict[str, List[AggScalar]] | ||
| # May be hitting https://github.com/python/mypy/issues/5958 | ||
| # saying it doesn't have an attribute __name__ | ||
| aggspec: DefaultDict = defaultdict(list) | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| order = [] | ||
| columns, pairs = list(zip(*kwargs.items())) | ||
|
|
||
| for name, (column, aggfunc) in zip(columns, pairs): | ||
| aggspec[column].append(aggfunc) | ||
| order.append((column, com.get_callable_name(aggfunc) or aggfunc)) | ||
|
|
||
| # uniquify aggfunc name if duplicated in order list | ||
| uniquified_order = _make_unique_kwarg_list(order) | ||
|
|
||
| # GH 25719, due to aggspec will change the order of assigned columns in aggregation | ||
| # uniquified_aggspec will store uniquified order list and will compare it with order | ||
| # based on index | ||
| aggspec_order = [ | ||
| (column, com.get_callable_name(aggfunc) or aggfunc) | ||
| for column, aggfuncs in aggspec.items() | ||
| for aggfunc in aggfuncs | ||
| ] | ||
| uniquified_aggspec = _make_unique_kwarg_list(aggspec_order) | ||
|
|
||
| # get the new indice of columns by comparison | ||
| col_idx_order = Index(uniquified_aggspec).get_indexer(uniquified_order) | ||
| return aggspec, columns, col_idx_order | ||
|
|
||
|
|
||
| def _make_unique_kwarg_list( | ||
| seq: Sequence[Tuple[Any, Any]] | ||
| ) -> Sequence[Tuple[Any, Any]]: | ||
| """Uniquify aggfunc name of the pairs in the order list | ||
|
|
||
| Examples: | ||
| -------- | ||
| >>> kwarg_list = [('a', '<lambda>'), ('a', '<lambda>'), ('b', '<lambda>')] | ||
| >>> _make_unique_kwarg_list(kwarg_list) | ||
| [('a', '<lambda>_0'), ('a', '<lambda>_1'), ('b', '<lambda>')] | ||
| """ | ||
| return [ | ||
| (pair[0], "_".join([pair[1], str(seq[:i].count(pair))])) | ||
| if seq.count(pair) > 1 | ||
| else pair | ||
| for i, pair in enumerate(seq) | ||
| ] | ||
|
|
||
|
|
||
| # TODO: Can't use, because mypy doesn't like us setting __name__ | ||
| # error: "partial[Any]" has no attribute "__name__" | ||
| # the type is: | ||
| # typing.Sequence[Callable[..., ScalarResult]] | ||
| # -> typing.Sequence[Callable[..., ScalarResult]]: | ||
|
|
||
|
|
||
| def _managle_lambda_list(aggfuncs: Sequence[Any]) -> Sequence[Any]: | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Possibly mangle a list of aggfuncs. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| aggfuncs : Sequence | ||
|
|
||
| Returns | ||
| ------- | ||
| mangled: list-like | ||
| A new AggSpec sequence, where lambdas have been converted | ||
| to have unique names. | ||
|
|
||
| Notes | ||
| ----- | ||
| If just one aggfunc is passed, the name will not be mangled. | ||
| """ | ||
| if len(aggfuncs) <= 1: | ||
| # don't mangle for .agg([lambda x: .]) | ||
| return aggfuncs | ||
| i = 0 | ||
| mangled_aggfuncs = [] | ||
| for aggfunc in aggfuncs: | ||
| if com.get_callable_name(aggfunc) == "<lambda>": | ||
| aggfunc = partial(aggfunc) | ||
| aggfunc.__name__ = f"<lambda_{i}>" | ||
| i += 1 | ||
| mangled_aggfuncs.append(aggfunc) | ||
|
|
||
| return mangled_aggfuncs | ||
|
|
||
|
|
||
| def maybe_mangle_lambdas(agg_spec: Any) -> Any: | ||
| """ | ||
| Make new lambdas with unique names. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| agg_spec : Any | ||
| An argument to GroupBy.agg. | ||
| Non-dict-like `agg_spec` are pass through as is. | ||
| For dict-like `agg_spec` a new spec is returned | ||
| with name-mangled lambdas. | ||
|
|
||
| Returns | ||
| ------- | ||
| mangled : Any | ||
| Same type as the input. | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> maybe_mangle_lambdas('sum') | ||
| 'sum' | ||
| >>> maybe_mangle_lambdas([lambda: 1, lambda: 2]) # doctest: +SKIP | ||
| [<function __main__.<lambda_0>, | ||
| <function pandas...._make_lambda.<locals>.f(*args, **kwargs)>] | ||
| """ | ||
| is_dict = is_dict_like(agg_spec) | ||
| if not (is_dict or is_list_like(agg_spec)): | ||
| return agg_spec | ||
| mangled_aggspec = type(agg_spec)() # dict or OrderdDict | ||
|
|
||
| if is_dict: | ||
| for key, aggfuncs in agg_spec.items(): | ||
| if is_list_like(aggfuncs) and not is_dict_like(aggfuncs): | ||
| mangled_aggfuncs = _managle_lambda_list(aggfuncs) | ||
| else: | ||
| mangled_aggfuncs = aggfuncs | ||
|
|
||
| mangled_aggspec[key] = mangled_aggfuncs | ||
| else: | ||
| mangled_aggspec = _managle_lambda_list(agg_spec) | ||
|
|
||
| return mangled_aggspec | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.