Skip to content

Commit e963988

Browse files
committed
BUG: Series arithmetic operator cannot handle constant and ndarray #2574
1 parent ad249df commit e963988

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

pandas/core/series.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,16 @@ def _flex_method(op, name):
245245

246246
@Appender(doc)
247247
def f(self, other, level=None, fill_value=None):
248-
return self._binop(other, op, level=level, fill_value=fill_value)
248+
if isinstance(other, Series):
249+
return self._binop(other, op, level=level, fill_value=fill_value)
250+
elif isinstance(other, (np.ndarray, list, tuple)):
251+
if len(other) != len(self):
252+
raise ValueError('Lengths must be equal')
253+
return self._binop(Series(other, self.index), op,
254+
level=level, fill_value=fill_value)
255+
else:
256+
return Series(op(self.values, other), self.index,
257+
name=self.name)
249258

250259
f.__name__ = name
251260
return f

pandas/tests/test_series.py

+17
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,23 @@ def test_all_any(self):
14781478
self.assert_(not bool_series.all())
14791479
self.assert_(bool_series.any())
14801480

1481+
def test_op_method(self):
1482+
def _check_op(series, other, op, alt):
1483+
result = op(series, other)
1484+
expected = alt(series, other)
1485+
tm.assert_almost_equal(result, expected)
1486+
1487+
def check(series, other):
1488+
simple_ops = ['add', 'sub', 'mul']
1489+
1490+
for opname in simple_ops:
1491+
_check_op(series, other, getattr(Series, opname),
1492+
getattr(operator, opname))
1493+
1494+
check(self.ts, self.ts * 2)
1495+
check(self.ts, self.ts[::2])
1496+
check(self.ts, 5)
1497+
14811498
def test_operators(self):
14821499

14831500
def _check_op(series, other, op, pos_only=False):

0 commit comments

Comments
 (0)