Skip to content

Commit 1975080

Browse files
author
luke
committed
fix agg with one element func list given arg and kwargs
1 parent 710b83b commit 1975080

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pandas/core/apply.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ def __init__(
130130
def f(x):
131131
return func(x, *args, **kwargs)
132132

133+
# GH 50642
134+
elif (
135+
(args or kwargs)
136+
and not isinstance(func, str)
137+
and is_one_element_callable_list(func)
138+
):
139+
140+
def f(x):
141+
return func[0](x, *args, **kwargs)
142+
133143
else:
134144
f = func
135145

@@ -1206,6 +1216,11 @@ def is_multi_agg_with_relabel(**kwargs) -> bool:
12061216
)
12071217

12081218

1219+
def is_one_element_callable_list(func: AggFuncType | None) -> bool:
1220+
# GH 50642
1221+
return isinstance(func, list) and len(func) == 1
1222+
1223+
12091224
def normalize_keyword_aggregation(
12101225
kwargs: dict,
12111226
) -> tuple[dict, list[str], npt.NDArray[np.intp]]:

pandas/tests/apply/test_frame_apply_relabeling.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,15 @@ def test_agg_namedtuple():
9595
index=pd.Index(["foo", "bar", "cat"]),
9696
)
9797
tm.assert_frame_equal(result, expected)
98+
99+
100+
def test_agg_given_one_element_func_list_arg_kwargs():
101+
# GH 50642
102+
df = pd.DataFrame({"x": [1, 2, 3]})
103+
104+
def foo(x, a, b=2):
105+
return x + a + b
106+
107+
result = df.agg([foo], 0, 1, b=2)
108+
expected = pd.DataFrame({"x": [4, 5, 6]})
109+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)