Skip to content

Commit 6b09390

Browse files
committed
ENH: add pow/rpow to DataFrame. close #2190
1 parent 27e34a4 commit 6b09390

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

RELEASE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pandas 0.9.1
3333
- New `top` and `bottom` options for handling NAs in rank (#1508, #2159)
3434
- Add `where` and `mask` functions to DataFrame (#2109, #2151)
3535
- Add `at_time` and `between_time` functions to DataFrame (#2149)
36+
- Add flexible `pow` and `rpow` methods to DataFrame (#2190)
3637

3738
**API Changes**
3839

pandas/core/frame.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,11 +676,13 @@ def __contains__(self, key):
676676
mul = _arith_method(operator.mul, 'multiply')
677677
sub = _arith_method(operator.sub, 'subtract')
678678
div = divide = _arith_method(lambda x, y: x / y, 'divide')
679+
pow = _arith_method(operator.pow, 'pow')
679680

680681
radd = _arith_method(_radd_compat, 'radd')
681682
rmul = _arith_method(operator.mul, 'rmultiply')
682683
rsub = _arith_method(lambda x, y: y - x, 'rsubtract')
683684
rdiv = _arith_method(lambda x, y: y / x, 'rdivide')
685+
rpow = _arith_method(lambda x, y: y ** x, 'rpow')
684686

685687
__add__ = _arith_method(operator.add, '__add__', default_axis=None)
686688
__sub__ = _arith_method(operator.sub, '__sub__', default_axis=None)

pandas/tests/test_frame.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ def test_nested_exception(self):
13161316
df.index=l
13171317

13181318
try:
1319-
print df
1319+
repr(df)
13201320
except Exception,e:
13211321
self.assertNotEqual(type(e),UnboundLocalError)
13221322

@@ -3074,15 +3074,25 @@ def test_first_last_valid(self):
30743074
self.assert_(index == frame.index[-6])
30753075

30763076
def test_arith_flex_frame(self):
3077-
res_add = self.frame.add(self.frame)
3078-
res_sub = self.frame.sub(self.frame)
3079-
res_mul = self.frame.mul(self.frame)
3080-
res_div = self.frame.div(2 * self.frame)
3081-
3082-
assert_frame_equal(res_add, self.frame + self.frame)
3083-
assert_frame_equal(res_sub, self.frame - self.frame)
3084-
assert_frame_equal(res_mul, self.frame * self.frame)
3085-
assert_frame_equal(res_div, self.frame / (2 * self.frame))
3077+
ops = ['add', 'sub', 'mul', 'div', 'pow']
3078+
aliases = {'div': 'truediv'}
3079+
3080+
for op in ops:
3081+
alias = aliases.get(op, op)
3082+
f = getattr(operator, alias)
3083+
result = getattr(self.frame, op)(2 * self.frame)
3084+
exp = f(self.frame, 2 * self.frame)
3085+
assert_frame_equal(result, exp)
3086+
3087+
# res_add = self.frame.add(self.frame)
3088+
# res_sub = self.frame.sub(self.frame)
3089+
# res_mul = self.frame.mul(self.frame)
3090+
# res_div = self.frame.div(2 * self.frame)
3091+
3092+
# assert_frame_equal(res_add, self.frame + self.frame)
3093+
# assert_frame_equal(res_sub, self.frame - self.frame)
3094+
# assert_frame_equal(res_mul, self.frame * self.frame)
3095+
# assert_frame_equal(res_div, self.frame / (2 * self.frame))
30863096

30873097
const_add = self.frame.add(1)
30883098
assert_frame_equal(const_add, self.frame + 1)

0 commit comments

Comments
 (0)