-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF: Apply.apply_multiple #53362
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
rhshadrach
merged 6 commits into
pandas-dev:main
from
topper-123:ref_Apply.apply_multiple
May 25, 2023
Merged
REF: Apply.apply_multiple #53362
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d72f1be
REF: Apply.apply_multiple
topper-123 e25b1dd
fix typing
topper-123 d5fb4c4
add test
topper-123 cc0c9f8
Merge branch 'master' into ref_Apply.apply_multiple
topper-123 d3f3d21
Merge branch 'master' into ref_Apply.apply_multiple
topper-123 999256a
fix pre-commit
topper-123 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 |
---|---|---|
|
@@ -91,6 +91,9 @@ def frame_apply( | |
elif axis == 1: | ||
klass = FrameColumnApply | ||
|
||
_, func, _, _ = reconstruct_func(func, **kwargs) | ||
assert func is not None | ||
|
||
return klass( | ||
obj, | ||
func, | ||
|
@@ -103,11 +106,13 @@ def frame_apply( | |
|
||
class Apply(metaclass=abc.ABCMeta): | ||
axis: AxisInt | ||
orig_f: AggFuncType | ||
f: AggFuncType | ||
|
||
def __init__( | ||
self, | ||
obj: AggObjType, | ||
func, | ||
func: AggFuncType, | ||
raw: bool, | ||
result_type: str | None, | ||
*, | ||
|
@@ -127,6 +132,7 @@ def __init__( | |
|
||
self.result_type = result_type | ||
|
||
f: AggFuncType | ||
# curry if needed | ||
if ( | ||
(kwargs or args) | ||
|
@@ -135,13 +141,14 @@ def __init__( | |
): | ||
|
||
def f(x): | ||
assert callable(func) | ||
return func(x, *args, **kwargs) | ||
|
||
else: | ||
f = func | ||
|
||
self.orig_f: AggFuncType = func | ||
self.f: AggFuncType = f | ||
self.orig_f = func | ||
self.f = f | ||
|
||
@abc.abstractmethod | ||
def apply(self) -> DataFrame | Series: | ||
|
@@ -554,7 +561,20 @@ def apply_multiple(self) -> DataFrame | Series: | |
result: Series, DataFrame, or None | ||
Result when self.f is a list-like or dict-like, None otherwise. | ||
""" | ||
return self.obj.aggregate(self.f, self.axis, *self.args, **self.kwargs) | ||
if self.axis == 1 and isinstance(self.obj, ABCDataFrame): | ||
return self.obj.T.apply(self.f, 0, *self.args, **self.kwargs).T | ||
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. Needs to be 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. Yeah, good catch. Done. |
||
|
||
func = self.f | ||
kwargs = self.kwargs | ||
|
||
if is_dict_like(func): | ||
result = self.agg_dict_like() | ||
else: | ||
result = self.agg_list_like() | ||
|
||
result = reconstruct_and_relabel_result(result, func, **kwargs) | ||
|
||
return result | ||
|
||
def normalize_dictlike_arg( | ||
self, how: str, obj: DataFrame | Series, func: AggFuncTypeDict | ||
|
@@ -1201,7 +1221,7 @@ def transform(self): | |
|
||
def reconstruct_func( | ||
func: AggFuncType | None, **kwargs | ||
) -> tuple[bool, AggFuncType | None, list[str] | None, npt.NDArray[np.intp] | None]: | ||
) -> tuple[bool, AggFuncType, list[str] | None, npt.NDArray[np.intp] | None]: | ||
""" | ||
This is the internal function to reconstruct func given if there is relabeling | ||
or not and also normalize the keyword to get new order of columns. | ||
|
@@ -1256,6 +1276,7 @@ def reconstruct_func( | |
|
||
if relabeling: | ||
func, columns, order = normalize_keyword_aggregation(kwargs) | ||
assert func is not None | ||
|
||
return relabeling, func, columns, order | ||
|
||
|
@@ -1445,6 +1466,26 @@ def relabel_result( | |
return reordered_result_in_dict | ||
|
||
|
||
def reconstruct_and_relabel_result(result, func, **kwargs) -> DataFrame | Series: | ||
from pandas import DataFrame | ||
|
||
relabeling, func, columns, order = reconstruct_func(func, **kwargs) | ||
|
||
if relabeling: | ||
# This is to keep the order to columns occurrence unchanged, and also | ||
# keep the order of new columns occurrence unchanged | ||
|
||
# For the return values of reconstruct_func, if relabeling is | ||
# False, columns and order will be None. | ||
assert columns is not None | ||
assert order is not None | ||
|
||
result_in_dict = relabel_result(result, func, columns, order) | ||
result = DataFrame(result_in_dict, index=columns) | ||
|
||
return result | ||
|
||
|
||
# TODO: Can't use, because mypy doesn't like us setting __name__ | ||
# error: "partial[Any]" has no attribute "__name__" | ||
# the type is: | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.