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

CLN: ops cleanups #30530

Merged
merged 1 commit into from
Dec 30, 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
13 changes: 7 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5148,7 +5148,7 @@ def reorder_levels(self, order, axis=0):
# Arithmetic / combination related

def _combine_frame(self, other, func, fill_value=None, level=None):
this, other = self.align(other, join="outer", level=level, copy=False)
# at this point we have `self._indexed_same(other)`

if fill_value is None:
# since _arith_op may be called in a loop, avoid function call
Expand All @@ -5164,14 +5164,15 @@ def _arith_op(left, right):
left, right = ops.fill_binop(left, right, fill_value)
return func(left, right)

if ops.should_series_dispatch(this, other, func):
if ops.should_series_dispatch(self, other, func):
# iterate over columns
new_data = ops.dispatch_to_series(this, other, _arith_op)
new_data = ops.dispatch_to_series(self, other, _arith_op)
else:
with np.errstate(all="ignore"):
res_values = _arith_op(this.values, other.values)
new_data = dispatch_fill_zeros(func, this.values, other.values, res_values)
return this._construct_result(new_data)
res_values = _arith_op(self.values, other.values)
new_data = dispatch_fill_zeros(func, self.values, other.values, res_values)

return new_data

def _combine_match_index(self, other, func):
# at this point we have `self.index.equals(other.index)`
Expand Down
22 changes: 11 additions & 11 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def _get_op_name(op, special):
"""
opname = op.__name__.strip("_")
if special:
opname = "__{opname}__".format(opname=opname)
opname = f"__{opname}__"
return opname


Expand Down Expand Up @@ -385,7 +385,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 _combine_frame_series,
# We only get here if called via _combine_series_frame,
# in which case we specifically want to operate row-by-row
assert right.index.equals(left.columns)

Expand Down Expand Up @@ -603,9 +603,7 @@ def _combine_series_frame(self, other, func, fill_value=None, axis=None, level=N
result : DataFrame
"""
if fill_value is not None:
raise NotImplementedError(
"fill_value {fill} not supported.".format(fill=fill_value)
)
raise NotImplementedError(f"fill_value {fill_value} not supported.")

if axis is None:
# default axis is columns
Expand Down Expand Up @@ -661,15 +659,13 @@ def to_series(right):
else:
raise ValueError(
"Unable to coerce to DataFrame, shape "
"must be {req_shape}: given {given_shape}".format(
req_shape=left.shape, given_shape=right.shape
)
f"must be {left.shape}: given {right.shape}"
)

elif right.ndim > 2:
raise ValueError(
"Unable to coerce to Series/DataFrame, dim "
"must be <= 2: {dim}".format(dim=right.shape)
f"must be <= 2: {right.shape}"
)

elif is_list_like(right) and not isinstance(right, (ABCSeries, ABCDataFrame)):
Expand Down Expand Up @@ -702,7 +698,11 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
# Another DataFrame
pass_op = op if should_series_dispatch(self, other, op) else na_op
pass_op = pass_op if not is_logical else op
return self._combine_frame(other, pass_op, fill_value, level)

left, right = self.align(other, join="outer", level=level, copy=False)
new_data = left._combine_frame(right, pass_op, fill_value)
return left._construct_result(new_data)

elif isinstance(other, ABCSeries):
# For these values of `axis`, we end up dispatching to Series op,
# so do not want the masked op.
Expand Down Expand Up @@ -763,7 +763,7 @@ def _comp_method_FRAME(cls, op, special):
str_rep = _get_opstr(op)
op_name = _get_op_name(op, special)

@Appender("Wrapper for comparison method {name}".format(name=op_name))
@Appender(f"Wrapper for comparison method {op_name}")
def f(self, other):

other = _align_method_FRAME(self, other, axis=None)
Expand Down