Skip to content

Commit 5c2411d

Browse files
committed
Merge pull request #5352 from jtratner/add-axis-kwarg-to-series-flex
CLN: Add axis kwarg to Series wrapper for compat
2 parents f6f06aa + a859e3a commit 5c2411d

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

pandas/core/ops.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,9 @@ def _flex_method_SERIES(op, name, str_rep=None, default_axis=None,
663663
""" % name
664664

665665
@Appender(doc)
666-
def f(self, other, level=None, fill_value=None):
666+
def flex_wrapper(self, other, level=None, fill_value=None, axis=0):
667+
# validate axis
668+
self._get_axis_number(axis)
667669
if isinstance(other, pd.Series):
668670
return self._binop(other, op, level=level, fill_value=fill_value)
669671
elif isinstance(other, (pa.Array, pd.Series, list, tuple)):
@@ -675,8 +677,8 @@ def f(self, other, level=None, fill_value=None):
675677
return self._constructor(op(self.values, other),
676678
self.index).__finalize__(self)
677679

678-
f.__name__ = name
679-
return f
680+
flex_wrapper.__name__ = name
681+
return flex_wrapper
680682

681683
series_flex_funcs = dict(flex_arith_method=_flex_method_SERIES,
682684
radd_func=_radd_compat,

pandas/tests/test_series.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,21 +3241,31 @@ def _check_fill(meth, op, a, b, fill_value=0):
32413241
a = Series([nan, 1., 2., 3., nan], index=np.arange(5))
32423242
b = Series([nan, 1, nan, 3, nan, 4.], index=np.arange(6))
32433243

3244-
ops = [Series.add, Series.sub, Series.mul, Series.pow,
3245-
Series.truediv, Series.div]
3246-
equivs = [operator.add, operator.sub, operator.mul, operator.pow,
3247-
operator.truediv]
3244+
pairings = []
3245+
for op in ['add', 'sub', 'mul', 'pow', 'truediv', 'floordiv']:
3246+
fv = 0
3247+
lop = getattr(Series, op)
3248+
lequiv = getattr(operator, op)
3249+
rop = getattr(Series, 'r' + op)
3250+
# bind op at definition time...
3251+
requiv = lambda x, y, op=op: getattr(operator, op)(y, x)
3252+
pairings.append((lop, lequiv, fv))
3253+
pairings.append((rop, requiv, fv))
3254+
32483255
if compat.PY3:
3249-
equivs.append(operator.truediv)
3256+
pairings.append((Series.div, operator.truediv, 1))
3257+
pairings.append((Series.rdiv, lambda x, y: operator.truediv(y, x), 1))
32503258
else:
3251-
equivs.append(operator.div)
3252-
fillvals = [0, 0, 1, 1]
3259+
pairings.append((Series.div, operator.div, 1))
3260+
pairings.append((Series.rdiv, lambda x, y: operator.div(y, x), 1))
32533261

3254-
for op, equiv_op, fv in zip(ops, equivs, fillvals):
3262+
for op, equiv_op, fv in pairings:
32553263
result = op(a, b)
32563264
exp = equiv_op(a, b)
32573265
assert_series_equal(result, exp)
32583266
_check_fill(op, equiv_op, a, b, fill_value=fv)
3267+
# should accept axis=0 or axis='rows'
3268+
op(a, b, axis=0)
32593269

32603270
def test_combine_first(self):
32613271
values = tm.makeIntIndex(20).values.astype(float)

0 commit comments

Comments
 (0)