Skip to content

Commit a4e64fc

Browse files
jbrockmendeljreback
authored andcommitted
REF: prepare Series arith op to be refactored to array op (#28413)
1 parent 810fa77 commit a4e64fc

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

pandas/core/ops/__init__.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@
4040

4141
from pandas._typing import ArrayLike
4242
from pandas.core.construction import array, extract_array
43-
from pandas.core.ops.array_ops import comp_method_OBJECT_ARRAY, define_na_arithmetic_op
43+
from pandas.core.ops.array_ops import (
44+
comp_method_OBJECT_ARRAY,
45+
define_na_arithmetic_op,
46+
na_arithmetic_op,
47+
)
4448
from pandas.core.ops.docstrings import (
4549
_arith_doc_FRAME,
4650
_flex_comp_doc_FRAME,
@@ -627,12 +631,13 @@ def _arith_method_SERIES(cls, op, special):
627631
_construct_divmod_result if op in [divmod, rdivmod] else _construct_result
628632
)
629633

630-
na_op = define_na_arithmetic_op(op, str_rep, eval_kwargs)
631-
632634
def wrapper(left, right):
633635
if isinstance(right, ABCDataFrame):
634636
return NotImplemented
635637

638+
left, right = _align_method_SERIES(left, right)
639+
res_name = get_op_result_name(left, right)
640+
636641
keep_null_freq = isinstance(
637642
right,
638643
(
@@ -644,9 +649,6 @@ def wrapper(left, right):
644649
),
645650
)
646651

647-
left, right = _align_method_SERIES(left, right)
648-
res_name = get_op_result_name(left, right)
649-
650652
lvalues = extract_array(left, extract_numpy=True)
651653
rvalues = extract_array(right, extract_numpy=True)
652654

@@ -659,7 +661,7 @@ def wrapper(left, right):
659661

660662
else:
661663
with np.errstate(all="ignore"):
662-
result = na_op(lvalues, rvalues)
664+
result = na_arithmetic_op(lvalues, rvalues, op, str_rep, eval_kwargs)
663665

664666
# We do not pass dtype to ensure that the Series constructor
665667
# does inference in the case where `result` has object-dtype.

pandas/core/ops/array_ops.py

+27-21
Original file line numberDiff line numberDiff line change
@@ -98,31 +98,37 @@ def masked_arith_op(x, y, op):
9898

9999
def define_na_arithmetic_op(op, str_rep, eval_kwargs):
100100
def na_op(x, y):
101-
"""
102-
Return the result of evaluating op on the passed in values.
101+
return na_arithmetic_op(x, y, op, str_rep, eval_kwargs)
103102

104-
If native types are not compatible, try coersion to object dtype.
103+
return na_op
105104

106-
Parameters
107-
----------
108-
x : array-like
109-
y : array-like or scalar
110105

111-
Returns
112-
-------
113-
array-like
106+
def na_arithmetic_op(left, right, op, str_rep, eval_kwargs):
107+
"""
108+
Return the result of evaluating op on the passed in values.
114109
115-
Raises
116-
------
117-
TypeError : invalid operation
118-
"""
119-
import pandas.core.computation.expressions as expressions
110+
If native types are not compatible, try coersion to object dtype.
120111
121-
try:
122-
result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs)
123-
except TypeError:
124-
result = masked_arith_op(x, y, op)
112+
Parameters
113+
----------
114+
left : np.ndarray
115+
right : np.ndarray or scalar
116+
str_rep : str or None
117+
eval_kwargs : kwargs to pass to expressions
118+
119+
Returns
120+
-------
121+
array-like
122+
123+
Raises
124+
------
125+
TypeError : invalid operation
126+
"""
127+
import pandas.core.computation.expressions as expressions
125128

126-
return missing.dispatch_fill_zeros(op, x, y, result)
129+
try:
130+
result = expressions.evaluate(op, str_rep, left, right, **eval_kwargs)
131+
except TypeError:
132+
result = masked_arith_op(left, right, op)
127133

128-
return na_op
134+
return missing.dispatch_fill_zeros(op, left, right, result)

0 commit comments

Comments
 (0)