Skip to content

Commit c99dd3b

Browse files
committed
REF: Apply.apply_multiple (pandas-dev#53362)
1 parent 65a0e60 commit c99dd3b

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

pandas/core/apply.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ def frame_apply(
9191
elif axis == 1:
9292
klass = FrameColumnApply
9393

94+
_, func, _, _ = reconstruct_func(func, **kwargs)
95+
assert func is not None
96+
9497
return klass(
9598
obj,
9699
func,
@@ -547,7 +550,20 @@ def apply_multiple(self) -> DataFrame | Series:
547550
result: Series, DataFrame, or None
548551
Result when self.f is a list-like or dict-like, None otherwise.
549552
"""
550-
return self.obj.aggregate(self.f, self.axis, *self.args, **self.kwargs)
553+
if self.axis == 1 and isinstance(self.obj, ABCDataFrame):
554+
return self.obj.T.apply(self.f, 0, args=self.args, **self.kwargs).T
555+
556+
func = self.f
557+
kwargs = self.kwargs
558+
559+
if is_dict_like(func):
560+
result = self.agg_dict_like()
561+
else:
562+
result = self.agg_list_like()
563+
564+
result = reconstruct_and_relabel_result(result, func, **kwargs)
565+
566+
return result
551567

552568
def normalize_dictlike_arg(
553569
self, how: str, obj: DataFrame | Series, func: AggFuncTypeDict
@@ -1439,6 +1455,26 @@ def relabel_result(
14391455
return reordered_result_in_dict
14401456

14411457

1458+
def reconstruct_and_relabel_result(result, func, **kwargs) -> DataFrame | Series:
1459+
from pandas import DataFrame
1460+
1461+
relabeling, func, columns, order = reconstruct_func(func, **kwargs)
1462+
1463+
if relabeling:
1464+
# This is to keep the order to columns occurrence unchanged, and also
1465+
# keep the order of new columns occurrence unchanged
1466+
1467+
# For the return values of reconstruct_func, if relabeling is
1468+
# False, columns and order will be None.
1469+
assert columns is not None
1470+
assert order is not None
1471+
1472+
result_in_dict = relabel_result(result, func, columns, order)
1473+
result = DataFrame(result_in_dict, index=columns)
1474+
1475+
return result
1476+
1477+
14421478
# TODO: Can't use, because mypy doesn't like us setting __name__
14431479
# error: "partial[Any]" has no attribute "__name__"
14441480
# the type is:

pandas/core/frame.py

+2-19
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@
122122
roperator,
123123
)
124124
from pandas.core.accessor import CachedAccessor
125-
from pandas.core.apply import (
126-
reconstruct_func,
127-
relabel_result,
128-
)
125+
from pandas.core.apply import reconstruct_and_relabel_result
129126
from pandas.core.array_algos.take import take_2d_multi
130127
from pandas.core.arraylike import OpsMixin
131128
from pandas.core.arrays import (
@@ -9579,23 +9576,9 @@ def aggregate(self, func=None, axis: Axis = 0, *args, **kwargs):
95799576

95809577
axis = self._get_axis_number(axis)
95819578

9582-
relabeling, func, columns, order = reconstruct_func(func, **kwargs)
9583-
95849579
op = frame_apply(self, func=func, axis=axis, args=args, kwargs=kwargs)
95859580
result = op.agg()
9586-
9587-
if relabeling:
9588-
# This is to keep the order to columns occurrence unchanged, and also
9589-
# keep the order of new columns occurrence unchanged
9590-
9591-
# For the return values of reconstruct_func, if relabeling is
9592-
# False, columns and order will be None.
9593-
assert columns is not None
9594-
assert order is not None
9595-
9596-
result_in_dict = relabel_result(result, func, columns, order)
9597-
result = DataFrame(result_in_dict, index=columns)
9598-
9581+
result = reconstruct_and_relabel_result(result, func, **kwargs)
95999582
return result
96009583

96019584
agg = aggregate

pandas/tests/apply/test_frame_apply.py

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ def test_apply(float_frame):
3737
assert result.index is float_frame.index
3838

3939

40+
@pytest.mark.parametrize("axis", [0, 1])
41+
def test_apply_args(float_frame, axis):
42+
result = float_frame.apply(lambda x, y: x + y, axis, args=(1,))
43+
expected = float_frame + 1
44+
tm.assert_frame_equal(result, expected)
45+
46+
4047
def test_apply_categorical_func():
4148
# GH 9573
4249
df = DataFrame({"c0": ["A", "A", "B", "B"], "c1": ["C", "C", "D", "D"]})

0 commit comments

Comments
 (0)