Skip to content

Commit c29e395

Browse files
authored
CLN: remove _combine_series_frame (#34374)
1 parent a64162f commit c29e395

File tree

3 files changed

+26
-45
lines changed

3 files changed

+26
-45
lines changed

pandas/core/computation/expressions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def evaluate(op, a, b, use_numexpr: bool = True):
223223
use_numexpr : bool, default True
224224
Whether to try to use numexpr.
225225
"""
226-
op_str = _op_str_mapping.get(op, None)
226+
op_str = _op_str_mapping[op]
227227
if op_str is not None:
228228
use_numexpr = use_numexpr and _bool_arith_check(op_str, a, b)
229229
if use_numexpr:

pandas/core/ops/__init__.py

+7-35
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,8 @@ def dispatch_to_series(left, right, func, axis=None):
280280
bm = left._mgr.operate_blockwise(right._mgr, array_op)
281281
return type(left)(bm)
282282

283-
elif isinstance(right, ABCSeries) and axis == "columns":
284-
# We only get here if called via _combine_series_frame,
285-
# in which case we specifically want to operate row-by-row
283+
elif isinstance(right, ABCSeries) and axis == 1:
284+
# axis=1 means we want to operate row-by-row
286285
assert right.index.equals(left.columns)
287286

288287
if right.dtype == "timedelta64[ns]":
@@ -292,6 +291,8 @@ def dispatch_to_series(left, right, func, axis=None):
292291
right = np.asarray(right)
293292
else:
294293
right = right._values
294+
# maybe_align_as_frame ensures we do not have an ndarray here
295+
assert not isinstance(right, np.ndarray)
295296

296297
arrays = [array_op(l, r) for l, r in zip(left._iter_column_arrays(), right)]
297298

@@ -440,35 +441,6 @@ def flex_wrapper(self, other, level=None, fill_value=None, axis=0):
440441
# DataFrame
441442

442443

443-
def _combine_series_frame(left, right, func, axis: int):
444-
"""
445-
Apply binary operator `func` to self, other using alignment and fill
446-
conventions determined by the axis argument.
447-
448-
Parameters
449-
----------
450-
left : DataFrame
451-
right : Series
452-
func : binary operator
453-
axis : {0, 1}
454-
455-
Returns
456-
-------
457-
result : DataFrame or Dict[int, Series[]]
458-
"""
459-
# We assume that self.align(other, ...) has already been called
460-
461-
rvalues = right._values
462-
assert not isinstance(rvalues, np.ndarray) # handled by align_series_as_frame
463-
464-
if axis == 0:
465-
new_data = dispatch_to_series(left, right, func)
466-
else:
467-
new_data = dispatch_to_series(left, right, func, axis="columns")
468-
469-
return new_data
470-
471-
472444
def _align_method_FRAME(
473445
left, right, axis, flex: Optional[bool] = False, level: Level = None
474446
):
@@ -671,7 +643,7 @@ def f(self, other, axis=default_axis, level=None, fill_value=None):
671643

672644
elif isinstance(other, ABCSeries):
673645
axis = self._get_axis_number(axis) if axis is not None else 1
674-
new_data = _combine_series_frame(self, other, op, axis=axis)
646+
new_data = dispatch_to_series(self, other, op, axis=axis)
675647
else:
676648
# in this case we always have `np.ndim(other) == 0`
677649
if fill_value is not None:
@@ -707,7 +679,7 @@ def f(self, other, axis=default_axis, level=None):
707679

708680
elif isinstance(other, ABCSeries):
709681
axis = self._get_axis_number(axis) if axis is not None else 1
710-
new_data = _combine_series_frame(self, other, op, axis=axis)
682+
new_data = dispatch_to_series(self, other, op, axis=axis)
711683
else:
712684
# in this case we always have `np.ndim(other) == 0`
713685
new_data = dispatch_to_series(self, other, op)
@@ -730,7 +702,7 @@ def f(self, other):
730702
self, other, axis=None, level=None, flex=False
731703
)
732704

733-
axis = "columns" # only relevant for Series other case
705+
axis = 1 # only relevant for Series other case
734706
# See GH#4537 for discussion of scalar op behavior
735707
new_data = dispatch_to_series(self, other, op, axis=axis)
736708
return self._construct_result(new_data)

pandas/core/ops/array_ops.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,7 @@ def masked_arith_op(x: np.ndarray, y, op):
8585
yrav = y.ravel()
8686
mask = notna(xrav) & ymask.ravel()
8787

88-
if yrav.shape != mask.shape:
89-
# FIXME: GH#5284, GH#5035, GH#19448
90-
# Without specifically raising here we get mismatched
91-
# errors in Py3 (TypeError) vs Py2 (ValueError)
92-
# Note: Only = an issue in DataFrame case
93-
raise ValueError("Cannot broadcast operands together.")
94-
88+
# See GH#5284, GH#5035, GH#19448 for historical reference
9589
if mask.any():
9690
with np.errstate(all="ignore"):
9791
result[mask] = op(xrav[mask], yrav[mask])
@@ -378,13 +372,28 @@ def get_array_op(op):
378372
# TODO: avoid getting here
379373
return op
380374

381-
op_name = op.__name__.strip("_")
375+
op_name = op.__name__.strip("_").lstrip("r")
376+
if op_name == "arith_op":
377+
# Reached via DataFrame._combine_frame
378+
return op
379+
382380
if op_name in {"eq", "ne", "lt", "le", "gt", "ge"}:
383381
return partial(comparison_op, op=op)
384382
elif op_name in {"and", "or", "xor", "rand", "ror", "rxor"}:
385383
return partial(logical_op, op=op)
386-
else:
384+
elif op_name in {
385+
"add",
386+
"sub",
387+
"mul",
388+
"truediv",
389+
"floordiv",
390+
"mod",
391+
"divmod",
392+
"pow",
393+
}:
387394
return partial(arithmetic_op, op=op)
395+
else:
396+
raise NotImplementedError(op_name)
388397

389398

390399
def maybe_upcast_datetimelike_array(obj: ArrayLike) -> ArrayLike:

0 commit comments

Comments
 (0)