Skip to content
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

REF: Consolidate alignment calls in DataFrame ops #28638

Merged
merged 3 commits into from
Oct 2, 2019
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
19 changes: 6 additions & 13 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5271,24 +5271,17 @@ def _arith_op(left, right):
new_data = dispatch_fill_zeros(func, this.values, other.values, res_values)
return this._construct_result(new_data)

def _combine_match_index(self, other, func, level=None):
left, right = self.align(other, join="outer", axis=0, level=level, copy=False)
# at this point we have `left.index.equals(right.index)`
def _combine_match_index(self, other, func):
# at this point we have `self.index.equals(other.index)`

if left._is_mixed_type or right._is_mixed_type:
if self._is_mixed_type or other._is_mixed_type:
# operate column-wise; avoid costly object-casting in `.values`
new_data = ops.dispatch_to_series(left, right, func)
new_data = ops.dispatch_to_series(self, other, func)
else:
# fastpath --> operate directly on values
with np.errstate(all="ignore"):
new_data = func(left.values.T, right.values).T
return left._construct_result(new_data)

def _combine_match_columns(self, other: Series, func, level=None):
left, right = self.align(other, join="outer", axis=1, level=level, copy=False)
# at this point we have `left.columns.equals(right.index)`
new_data = ops.dispatch_to_series(left, right, func, axis="columns")
return left._construct_result(new_data)
new_data = func(self.values.T, other.values).T
return new_data

def _construct_result(self, result) -> "DataFrame":
"""
Expand Down
21 changes: 12 additions & 9 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def column_op(a, b):
return {i: func(a.iloc[:, i], b.iloc[:, i]) for i in range(len(a.columns))}

elif isinstance(right, ABCSeries) and axis == "columns":
# We only get here if called via left._combine_match_columns,
# We only get here if called via _combine_frame_series,
# in which case we specifically want to operate row-by-row
assert right.index.equals(left.columns)

Expand Down Expand Up @@ -597,15 +597,18 @@ def _combine_series_frame(self, other, func, fill_value=None, axis=None, level=N
"fill_value {fill} not supported.".format(fill=fill_value)
)

if axis is not None:
axis = self._get_axis_number(axis)
if axis == 0:
return self._combine_match_index(other, func, level=level)
else:
return self._combine_match_columns(other, func, level=level)
if axis is None:
# default axis is columns
axis = 1

axis = self._get_axis_number(axis)
left, right = self.align(other, join="outer", axis=axis, level=level, copy=False)
if axis == 0:
new_data = left._combine_match_index(right, func)
else:
new_data = dispatch_to_series(left, right, func, axis="columns")

# default axis is columns
return self._combine_match_columns(other, func, level=level)
return left._construct_result(new_data)


def _align_method_FRAME(left, right, axis):
Expand Down