Skip to content

Commit 66d376d

Browse files
jbrockmendelaeltanawy
authored andcommitted
Use dispatch_to_series where possible (pandas-dev#22572)
1 parent 3141dfe commit 66d376d

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

pandas/core/frame.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -4818,13 +4818,14 @@ def _arith_op(left, right):
48184818
return ops.dispatch_to_series(this, other, _arith_op)
48194819
else:
48204820
result = _arith_op(this.values, other.values)
4821-
4822-
return self._constructor(result, index=new_index, columns=new_columns,
4823-
copy=False)
4821+
return self._constructor(result,
4822+
index=new_index, columns=new_columns,
4823+
copy=False)
48244824

48254825
def _combine_match_index(self, other, func, level=None):
48264826
left, right = self.align(other, join='outer', axis=0, level=level,
48274827
copy=False)
4828+
assert left.index.equals(right.index)
48284829
new_data = func(left.values.T, right.values).T
48294830
return self._constructor(new_data,
48304831
index=left.index, columns=self.columns,
@@ -4833,6 +4834,7 @@ def _combine_match_index(self, other, func, level=None):
48334834
def _combine_match_columns(self, other, func, level=None, try_cast=True):
48344835
left, right = self.align(other, join='outer', axis=1, level=level,
48354836
copy=False)
4837+
assert left.columns.equals(right.index)
48364838

48374839
new_data = left._data.eval(func=func, other=right,
48384840
axes=[left.columns, self.index],
@@ -4841,12 +4843,7 @@ def _combine_match_columns(self, other, func, level=None, try_cast=True):
48414843

48424844
def _combine_const(self, other, func, errors='raise', try_cast=True):
48434845
if lib.is_scalar(other) or np.ndim(other) == 0:
4844-
new_data = {i: func(self.iloc[:, i], other)
4845-
for i, col in enumerate(self.columns)}
4846-
4847-
result = self._constructor(new_data, index=self.index, copy=False)
4848-
result.columns = self.columns
4849-
return result
4846+
return ops.dispatch_to_series(self, other, func)
48504847

48514848
new_data = self._data.eval(func=func, other=other,
48524849
errors=errors,

pandas/core/ops.py

+1
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,7 @@ def dispatch_to_series(left, right, func):
16381638
"""
16391639
# Note: we use iloc to access columns for compat with cases
16401640
# with non-unique columns.
1641+
right = lib.item_from_zerodim(right)
16411642
if lib.is_scalar(right):
16421643
new_data = {i: func(left.iloc[:, i], right)
16431644
for i in range(len(left.columns))}

0 commit comments

Comments
 (0)