8000 REF: Consolidate alignment calls in DataFrame ops by jbrockmendel · Pull Request #28638 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

REF: Consolidate alignment calls in DataFrame ops #28638

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 3 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
REF: simplify _combine_series_frame, unify align calls
  • Loading branch information
jbrockmendel committed Sep 26, 2019
commit f75156722573ccaf68232262aaa549f93431fb7b
24 changes: 11 additions & 13 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5271,24 +5271,22 @@ 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 self._construct_result(new_data)

def _combine_match_columns(self, other: Series, func):
# at this point we have `self.columns.equals(other.index)`
new_data = ops.dispatch_to_series(self, other, func, axis="columns")
return self._construct_result(new_data)

def _construct_result(self, result) -> "DataFrame":
"""
Expand Down
19 changes: 10 additions & 9 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,15 +727,16 @@ 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)

# default axis is columns
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:
return left._combine_match_index(right, func)
else:
return left._combine_match_columns(right, func)


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