Skip to content

Commit

Permalink
ENH: can pass args, kwds to DataFrame.apply, GH #376
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Nov 25, 2011
1 parent c1307b6 commit 0505033
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2773,6 +2773,28 @@ def _checkit(axis=0, raw=False):
result = no_cols.apply(lambda x: x.mean(), broadcast=True)
self.assert_(isinstance(result, DataFrame))

def test_apply_with_args_kwds(self):
def add_some(x, howmuch=0):
return x + howmuch

def agg_and_add(x, howmuch=0):
return x.mean() + howmuch

def subtract_and_divide(x, sub, divide=1):
return (x - sub) / divide

result = self.frame.apply(add_some, howmuch=2)
exp = self.frame.apply(lambda x: x + 2)
assert_frame_equal(result, exp)

result = self.frame.apply(agg_and_add, howmuch=2)
exp = self.frame.apply(lambda x: x.mean() + 2)
assert_series_equal(result, exp)

res = self.frame.apply(subtract_and_divide, args=(2,), divide=2)
exp = self.frame.apply(lambda x: (x - 2.) / 2.)
assert_frame_equal(res, exp)

def test_applymap(self):
applied = self.frame.applymap(lambda x: x * 2)
assert_frame_equal(applied, self.frame * 2)
Expand Down

0 comments on commit 0505033

Please sign in to comment.