Skip to content

Commit cf99831

Browse files
jbrockmendeljreback
authored andcommitted
CLN: ops cleanups (#30530)
1 parent c76f810 commit cf99831

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

pandas/core/frame.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5148,7 +5148,7 @@ def reorder_levels(self, order, axis=0):
51485148
# Arithmetic / combination related
51495149

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

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

5167-
if ops.should_series_dispatch(this, other, func):
5167+
if ops.should_series_dispatch(self, other, func):
51685168
# iterate over columns
5169-
new_data = ops.dispatch_to_series(this, other, _arith_op)
5169+
new_data = ops.dispatch_to_series(self, other, _arith_op)
51705170
else:
51715171
with np.errstate(all="ignore"):
5172-
res_values = _arith_op(this.values, other.values)
5173-
new_data = dispatch_fill_zeros(func, this.values, other.values, res_values)
5174-
return this._construct_result(new_data)
5172+
res_values = _arith_op(self.values, other.values)
5173+
new_data = dispatch_fill_zeros(func, self.values, other.values, res_values)
5174+
5175+
return new_data
51755176

51765177
def _combine_match_index(self, other, func):
51775178
# at this point we have `self.index.equals(other.index)`

pandas/core/ops/__init__.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def _get_op_name(op, special):
302302
"""
303303
opname = op.__name__.strip("_")
304304
if special:
305-
opname = "__{opname}__".format(opname=opname)
305+
opname = f"__{opname}__"
306306
return opname
307307

308308

@@ -385,7 +385,7 @@ def column_op(a, b):
385385
return {i: func(a.iloc[:, i], b.iloc[:, i]) for i in range(len(a.columns))}
386386

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

@@ -603,9 +603,7 @@ def _combine_series_frame(self, other, func, fill_value=None, axis=None, level=N
603603
result : DataFrame
604604
"""
605605
if fill_value is not None:
606-
raise NotImplementedError(
607-
"fill_value {fill} not supported.".format(fill=fill_value)
608-
)
606+
raise NotImplementedError(f"fill_value {fill_value} not supported.")
609607

610608
if axis is None:
611609
# default axis is columns
@@ -661,15 +659,13 @@ def to_series(right):
661659
else:
662660
raise ValueError(
663661
"Unable to coerce to DataFrame, shape "
664-
"must be {req_shape}: given {given_shape}".format(
665-
req_shape=left.shape, given_shape=right.shape
666-
)
662+
f"must be {left.shape}: given {right.shape}"
667663
)
668664

669665
elif right.ndim > 2:
670666
raise ValueError(
671667
"Unable to coerce to Series/DataFrame, dim "
672-
"must be <= 2: {dim}".format(dim=right.shape)
668+
f"must be <= 2: {right.shape}"
673669
)
674670

675671
elif is_list_like(right) and not isinstance(right, (ABCSeries, ABCDataFrame)):
@@ -702,7 +698,11 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
702698
# Another DataFrame
703699
pass_op = op if should_series_dispatch(self, other, op) else na_op
704700
pass_op = pass_op if not is_logical else op
705-
return self._combine_frame(other, pass_op, fill_value, level)
701+
702+
left, right = self.align(other, join="outer", level=level, copy=False)
703+
new_data = left._combine_frame(right, pass_op, fill_value)
704+
return left._construct_result(new_data)
705+
706706
elif isinstance(other, ABCSeries):
707707
# For these values of `axis`, we end up dispatching to Series op,
708708
# so do not want the masked op.
@@ -763,7 +763,7 @@ def _comp_method_FRAME(cls, op, special):
763763
str_rep = _get_opstr(op)
764764
op_name = _get_op_name(op, special)
765765

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

769769
other = _align_method_FRAME(self, other, axis=None)

0 commit comments

Comments
 (0)