-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: Fix agg ingore arg/kwargs when given list like func #50863
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
Changes from 24 commits
86f1fd2
0ceb01d
b0d23a0
76a675c
d1a4d17
abe58c8
5ce782a
c1e48b7
18c4f9e
0daad25
eab348c
54d27e1
0a3d2ea
b98b35e
55f1376
234cff6
6957a7c
a3ef451
1d46712
6f28f2f
b4056ac
279139c
7eb2c94
d85ec33
371c068
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1468,3 +1468,49 @@ def test_agg_of_mode_list(test, constant): | |
expected = expected.set_index(0) | ||
|
||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test__dataframe_groupy_agg_list_like_func_with_args(): | ||
# GH 50624 | ||
df = DataFrame({"x": [1, 2, 3], "y": ["a", "b", "c"]}) | ||
df = df.groupby("y") | ||
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. Can you call this |
||
|
||
def foo1(x, a=1, c=0): | ||
return x.sum() + a + c | ||
|
||
def foo2(x, b=2, c=0): | ||
return x.sum() + b + c | ||
|
||
msg = r"foo1\(\) got an unexpected keyword argument 'b'" | ||
with pytest.raises(TypeError, match=msg): | ||
df.agg([foo1, foo2], 3, b=3, c=4) | ||
|
||
result = df.agg([foo1, foo2], 3, c=4) | ||
expected = DataFrame( | ||
[[8, 8], [9, 9], [10, 10]], | ||
index=Index(["a", "b", "c"], name="y"), | ||
columns=MultiIndex.from_tuples([("x", "foo1"), ("x", "foo2")]), | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test__series_groupy_agg_list_like_func_with_args(): | ||
# GH 50624 | ||
data = Series([1, 2, 3]) | ||
data = data.groupby(data) | ||
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. Same. |
||
|
||
def foo1(x, a=1, c=0): | ||
return x.sum() + a + c | ||
|
||
def foo2(x, b=2, c=0): | ||
return x.sum() + b + c | ||
|
||
msg = r"foo1\(\) got an unexpected keyword argument 'b'" | ||
with pytest.raises(TypeError, match=msg): | ||
data.agg([foo1, foo2], 3, b=3, c=4) | ||
|
||
result = data.agg([foo1, foo2], 3, c=4) | ||
expected = DataFrame( | ||
[[8, 8], [9, 9], [10, 10]], index=Index([1, 2, 3]), columns=["foo1", "foo2"] | ||
) | ||
tm.assert_frame_equal(result, expected) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -633,6 +633,31 @@ def test_try_aggregate_non_existing_column(): | |
df.resample("30T").agg({"x": ["mean"], "y": ["median"], "z": ["sum"]}) | ||
|
||
|
||
def test_agg_list_like_func_with_args(): | ||
# 50624 | ||
df = DataFrame( | ||
{"x": [1, 2, 3]}, index=date_range("2020-01-01", periods=3, freq="D") | ||
) | ||
|
||
def foo1(x, a=1, c=0): | ||
return x + a + c | ||
|
||
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) | ||
|
||
result = df.agg([foo1, foo2], 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 test should be using resample. |
||
expected = DataFrame( | ||
[[8, 8], [9, 9], [10, 10]], | ||
index=date_range("2020-01-01", periods=3, freq="D"), | ||
columns=pd.MultiIndex.from_tuples([("x", "foo1"), ("x", "foo2")]), | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_selection_api_validation(): | ||
# GH 13500 | ||
index = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D") | ||
|
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 should have mentioned - this should be split up with the groupby and resampler part going under their section within Bugfixes.