Skip to content

Commit 0505033

Browse files
committed
ENH: can pass args, kwds to DataFrame.apply, GH #376
1 parent c1307b6 commit 0505033

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

pandas/tests/test_frame.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,6 +2773,28 @@ def _checkit(axis=0, raw=False):
27732773
result = no_cols.apply(lambda x: x.mean(), broadcast=True)
27742774
self.assert_(isinstance(result, DataFrame))
27752775

2776+
def test_apply_with_args_kwds(self):
2777+
def add_some(x, howmuch=0):
2778+
return x + howmuch
2779+
2780+
def agg_and_add(x, howmuch=0):
2781+
return x.mean() + howmuch
2782+
2783+
def subtract_and_divide(x, sub, divide=1):
2784+
return (x - sub) / divide
2785+
2786+
result = self.frame.apply(add_some, howmuch=2)
2787+
exp = self.frame.apply(lambda x: x + 2)
2788+
assert_frame_equal(result, exp)
2789+
2790+
result = self.frame.apply(agg_and_add, howmuch=2)
2791+
exp = self.frame.apply(lambda x: x.mean() + 2)
2792+
assert_series_equal(result, exp)
2793+
2794+
res = self.frame.apply(subtract_and_divide, args=(2,), divide=2)
2795+
exp = self.frame.apply(lambda x: (x - 2.) / 2.)
2796+
assert_frame_equal(res, exp)
2797+
27762798
def test_applymap(self):
27772799
applied = self.frame.applymap(lambda x: x * 2)
27782800
assert_frame_equal(applied, self.frame * 2)

0 commit comments

Comments
 (0)