Skip to content

REF: prepare Series comparison op to be refactored to array op #28396

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 1 commit into from
Sep 12, 2019
Merged
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
41 changes: 15 additions & 26 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,27 +663,9 @@ def _comp_method_SERIES(cls, op, special):
"""
op_name = _get_op_name(op, special)

def na_op(x, y):
# TODO:
# should have guarantees on what x, y can be type-wise
# Extension Dtypes are not called here

if is_object_dtype(x.dtype):
result = comp_method_OBJECT_ARRAY(op, x, y)

else:
method = getattr(x, op_name)
with np.errstate(all="ignore"):
result = method(y)
if result is NotImplemented:
return invalid_comparison(x, y, op)

return result

def wrapper(self, other):

res_name = get_op_result_name(self, other)
other = lib.item_from_zerodim(other)

# TODO: shouldn't we be applying finalize whenever
# not isinstance(other, ABCSeries)?
Expand All @@ -693,20 +675,19 @@ def wrapper(self, other):
else x
)

if isinstance(other, list):
# TODO: same for tuples?
other = np.asarray(other)

if isinstance(other, ABCDataFrame): # pragma: no cover
# Defer to DataFrame implementation; fail early
return NotImplemented

if isinstance(other, ABCSeries) and not self._indexed_same(other):
raise ValueError("Can only compare identically-labeled Series objects")

elif isinstance(
other, (np.ndarray, ABCExtensionArray, ABCIndexClass, ABCSeries)
):
other = lib.item_from_zerodim(other)
if isinstance(other, list):
# TODO: same for tuples?
other = np.asarray(other)

if isinstance(other, (np.ndarray, ABCExtensionArray, ABCIndexClass)):
# TODO: make this treatment consistent across ops and classes.
# We are not catching all listlikes here (e.g. frozenset, tuple)
# The ambiguous case is object-dtype. See GH#27803
Expand All @@ -726,9 +707,17 @@ def wrapper(self, other):
else:
res_values = np.zeros(len(lvalues), dtype=bool)

elif is_object_dtype(lvalues.dtype):
res_values = comp_method_OBJECT_ARRAY(op, lvalues, rvalues)

else:
op_name = "__{op}__".format(op=op.__name__)
method = getattr(lvalues, op_name)
with np.errstate(all="ignore"):
res_values = na_op(lvalues, rvalues)
res_values = method(rvalues)

if res_values is NotImplemented:
res_values = invalid_comparison(lvalues, rvalues, op)
if is_scalar(res_values):
raise TypeError(
"Could not compare {typ} type with Series".format(typ=type(rvalues))
Expand Down