-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
DEPR: make Series.agg aggregate when possible #53325
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8c3745b
BUG: make Series.agg aggregate when possible
topper-123 57b76eb
fix doc build
topper-123 afb0bf8
deprecate instead of treating as a bug
topper-123 9bb2b87
CLN: Apply.agg_list_like
topper-123 2e54b05
some cleanups
topper-123 c292c5b
REF/CLN: func in core.apply (#53437)
rhshadrach 13ba267
REF: Decouple Series.apply from Series.agg (#53400)
topper-123 ff7c64b
update test
topper-123 78c133a
fix issues
topper-123 fbc3092
fix issues
topper-123 a0e319a
fix issues
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 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 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 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 |
---|---|---|
|
@@ -1478,8 +1478,8 @@ def test_any_apply_keyword_non_zero_axis_regression(): | |
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_agg_list_like_func_with_args(): | ||
# GH 50624 | ||
def test_agg_mapping_func_deprecated(): | ||
# GH 53325 | ||
df = DataFrame({"x": [1, 2, 3]}) | ||
|
||
def foo1(x, a=1, c=0): | ||
|
@@ -1488,17 +1488,26 @@ def foo1(x, a=1, c=0): | |
def foo2(x, b=2, c=0): | ||
return x + b + c | ||
|
||
msg = r"foo1\(\) got an unexpected keyword argument 'b'" | ||
with pytest.raises(TypeError, match=msg): | ||
df.agg([foo1, foo2], 0, 3, b=3, c=4) | ||
# single func already takes the vectorized path | ||
result = df.agg(foo1, 0, 3, c=4) | ||
expected = df + 7 | ||
tm.assert_frame_equal(result, expected) | ||
|
||
msg = "using .+ in Series.agg cannot aggregate and" | ||
|
||
result = df.agg([foo1, foo2], 0, 3, c=4) | ||
with tm.assert_produces_warning(FutureWarning, match=msg): | ||
result = df.agg([foo1, foo2], 0, 3, c=4) | ||
expected = DataFrame( | ||
[[8, 8], [9, 9], [10, 10]], | ||
columns=MultiIndex.from_tuples([("x", "foo1"), ("x", "foo2")]), | ||
[[8, 8], [9, 9], [10, 10]], columns=[["x", "x"], ["foo1", "foo2"]] | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# TODO: the result below is wrong, should be fixed (GH53325) | ||
with tm.assert_produces_warning(FutureWarning, match=msg): | ||
result = df.agg({"x": foo1}, 0, 3, c=4) | ||
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. This actually gave a wrong result in main and in v1.5, which was quite surprising. |
||
expected = DataFrame([2, 3, 4], columns=["x"]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_agg_std(): | ||
df = DataFrame(np.arange(6).reshape(3, 2), columns=["A", "B"]) | ||
|
This file contains 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 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 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 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.
Just a question about enforcing this deprecation; will we also check that
result
is a scalar-like? In theoryf
could non-reducinglambda *args, **kwargs: obj
?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.
I think we shouldn't. Rather, we treat whatever the UDF returns as if it were a scalar. An example where this can be useful:
While I think this is only particularly useful in groupby, for consistency I think we should apply this approach to all
agg
methods. It has the added benefit of not having to infer what is a scalar and what is not.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.
Yeah, This just deprecates a path that will never aggregate, but there are still other paths that won't aggregate, like you mention. I agree with @rhshadrach that a sensible path may be to just be very permissive, but OTOH if we want to restrict what we accept for the results fro aggregations, that can always be done later.