Skip to content

Use dispatch_to_series where possible #22572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4806,13 +4806,14 @@ def _arith_op(left, right):
return ops.dispatch_to_series(this, other, _arith_op)
else:
result = _arith_op(this.values, other.values)

return self._constructor(result, index=new_index, columns=new_columns,
copy=False)
return self._constructor(result,
index=new_index, columns=new_columns,
copy=False)

def _combine_match_index(self, other, func, level=None):
left, right = self.align(other, join='outer', axis=0, level=level,
copy=False)
assert left.index.equals(right.index)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need these assert statements? Are they tested anywhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions are always true. I added these so I don't have to keep looking back at core.ops (the only place from which these are called) to double-check what's going on.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is just a sanity check really? Perhaps we should comment that then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the assertion is self-explanatory, but if adding a comment makes reviewers happy, there’s no real downside.

new_data = func(left.values.T, right.values).T
return self._constructor(new_data,
index=left.index, columns=self.columns,
Expand All @@ -4821,6 +4822,7 @@ def _combine_match_index(self, other, func, level=None):
def _combine_match_columns(self, other, func, level=None, try_cast=True):
left, right = self.align(other, join='outer', axis=1, level=level,
copy=False)
assert left.columns.equals(right.index)

new_data = left._data.eval(func=func, other=right,
axes=[left.columns, self.index],
Expand All @@ -4829,12 +4831,7 @@ def _combine_match_columns(self, other, func, level=None, try_cast=True):

def _combine_const(self, other, func, errors='raise', try_cast=True):
if lib.is_scalar(other) or np.ndim(other) == 0:
new_data = {i: func(self.iloc[:, i], other)
for i, col in enumerate(self.columns)}

result = self._constructor(new_data, index=self.index, copy=False)
result.columns = self.columns
return result
return ops.dispatch_to_series(self, other, func)

new_data = self._data.eval(func=func, other=other,
errors=errors,
Expand Down
1 change: 1 addition & 0 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,7 @@ def dispatch_to_series(left, right, func):
"""
# Note: we use iloc to access columns for compat with cases
# with non-unique columns.
right = lib.item_from_zerodim(right)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason you needed to add this? (e.g. do we actually hit this in the new code path and not in the original)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This PR has DataFrame._combine_const send zero-dim arrays down this path, which without this line don't get handled correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok great

if lib.is_scalar(right):
new_data = {i: func(left.iloc[:, i], right)
for i in range(len(left.columns))}
Expand Down