Skip to content

Commit 4d3cd0d

Browse files
committed
ENH: add support for rmod
1 parent 37b3e61 commit 4d3cd0d

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

pandas/core/frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -842,12 +842,14 @@ def __contains__(self, key):
842842
sub = _arith_method(operator.sub, 'subtract', '-')
843843
div = divide = _arith_method(lambda x, y: x / y, 'divide', '/')
844844
pow = _arith_method(operator.pow, 'pow', '**')
845+
mod = _arith_method(lambda x, y: x % y, 'mod')
845846

846847
radd = _arith_method(_radd_compat, 'radd')
847848
rmul = _arith_method(operator.mul, 'rmultiply')
848849
rsub = _arith_method(lambda x, y: y - x, 'rsubtract')
849850
rdiv = _arith_method(lambda x, y: y / x, 'rdivide')
850851
rpow = _arith_method(lambda x, y: y ** x, 'rpow')
852+
rmod = _arith_method(lambda x, y: y % x, 'rmod')
851853

852854
__add__ = _arith_method(operator.add, '__add__', '+', default_axis=None)
853855
__sub__ = _arith_method(operator.sub, '__sub__', '-', default_axis=None)

pandas/core/series.py

+1
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,7 @@ def _binop(self, other, func, level=None, fill_value=None):
21272127
except AttributeError: # pragma: no cover
21282128
# Python 3
21292129
div = _flex_method(operator.truediv, 'divide')
2130+
mod = _flex_method(operator.mod, 'mod')
21302131

21312132
def combine(self, other, func, fill_value=nan):
21322133
"""

pandas/tests/test_frame.py

+15
Original file line numberDiff line numberDiff line change
@@ -4041,6 +4041,13 @@ def test_modulo(self):
40414041
result2 = DataFrame(p.values.astype('float64') % 0,index=p.index,columns=p.columns)
40424042
assert_frame_equal(result2,expected)
40434043

4044+
# not commutative with series
4045+
p = DataFrame(np.random.randn(10, 5))
4046+
s = p[0]
4047+
res = s % p
4048+
res2 = p % s
4049+
self.assertFalse(np.array_equal(res.fillna(0), res2.fillna(0)))
4050+
40444051
def test_div(self):
40454052

40464053
# integer div, but deal with the 0's
@@ -4062,6 +4069,12 @@ def test_div(self):
40624069
result2 = DataFrame(p.values.astype('float64')/0,index=p.index,columns=p.columns).fillna(np.inf)
40634070
assert_frame_equal(result2,expected)
40644071

4072+
p = DataFrame(np.random.randn(10, 5))
4073+
s = p[0]
4074+
res = s / p
4075+
res2 = p / s
4076+
self.assertFalse(np.array_equal(res.fillna(0), res2.fillna(0)))
4077+
40654078
def test_logical_operators(self):
40664079
import operator
40674080

@@ -4391,11 +4404,13 @@ def test_arith_flex_series(self):
43914404
assert_frame_equal(df.sub(row), df - row)
43924405
assert_frame_equal(df.div(row), df / row)
43934406
assert_frame_equal(df.mul(row), df * row)
4407+
assert_frame_equal(df.mod(row), df % row)
43944408

43954409
assert_frame_equal(df.add(col, axis=0), (df.T + col).T)
43964410
assert_frame_equal(df.sub(col, axis=0), (df.T - col).T)
43974411
assert_frame_equal(df.div(col, axis=0), (df.T / col).T)
43984412
assert_frame_equal(df.mul(col, axis=0), (df.T * col).T)
4413+
assert_frame_equal(df.mod(col, axis=0), (df.T % col).T)
43994414

44004415
def test_arith_non_pandas_object(self):
44014416
df = self.simple

pandas/tests/test_series.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,11 @@ def test_modulo(self):
17371737
expected = Series(p['first'].values % p['second'].values)
17381738
assert_series_equal(result,expected)
17391739

1740+
p = p.astype('float64')
1741+
result = p['first'] % p['second']
1742+
result2 = p['second'] % p['first']
1743+
self.assertFalse(np.array_equal(result,result2))
1744+
17401745
def test_div(self):
17411746

17421747
# integer div, but deal with the 0's
@@ -1761,6 +1766,7 @@ def test_div(self):
17611766
assert_series_equal(result,p['first'].astype('float64'))
17621767
else:
17631768
assert_series_equal(result,p['first'])
1769+
self.assertFalse(np.array_equal(result, p['second'] / p['first']))
17641770

17651771
def test_operators(self):
17661772

@@ -1773,7 +1779,7 @@ def _check_op(series, other, op, pos_only=False):
17731779
tm.assert_almost_equal(cython_or_numpy, python)
17741780

17751781
def check(series, other):
1776-
simple_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv']
1782+
simple_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv', 'mod']
17771783

17781784
for opname in simple_ops:
17791785
_check_op(series, other, getattr(operator, opname))
@@ -1787,6 +1793,7 @@ def check(series, other):
17871793
_check_op(series, other, lambda x, y: operator.mul(y, x))
17881794
_check_op(series, other, lambda x, y: operator.pow(y, x),
17891795
pos_only=True)
1796+
_check_op(series, other, lambda x, y: operator.mod(y, x))
17901797

17911798
check(self.ts, self.ts * 2)
17921799
check(self.ts, self.ts * 0)

0 commit comments

Comments
 (0)