Skip to content

CLN: small ops cleanup #28379

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 3 commits into from
Sep 11, 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
4 changes: 0 additions & 4 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
ensure_object,
is_bool_dtype,
is_datetime64_dtype,
is_datetimelike_v_numeric,
is_extension_array_dtype,
is_integer_dtype,
is_list_like,
Expand Down Expand Up @@ -672,9 +671,6 @@ def na_op(x, y):
if is_object_dtype(x.dtype):
result = comp_method_OBJECT_ARRAY(op, x, y)

elif is_datetimelike_v_numeric(x, y):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removal here is just coupled with some of the other items you've been working on to clean up the datetimelike ops right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to split cleanup changes off from bigger changes in preparation for a non-trivial refactor to fix arithmetic performance. This check is no longer necessary; I think it was put in place because of older numpy behavior.

return invalid_comparison(x, y, op)

else:
method = getattr(x, op_name)
with np.errstate(all="ignore"):
Expand Down
31 changes: 8 additions & 23 deletions pandas/core/ops/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,19 @@
from .roperator import rdivmod, rfloordiv, rmod


def fill_zeros(result, x, y, name, fill):
def fill_zeros(result, x, y):
"""
If this is a reversed op, then flip x,y

If we have an integer value (or array in y)
and we have 0's, fill them with the fill,
and we have 0's, fill them with np.nan,
return the result.

Mask the nan's from x.
"""
if fill is None or is_float_dtype(result.dtype):
if is_float_dtype(result.dtype):
return result

if name.startswith(("r", "__r")):
x, y = y, x

is_variable_type = hasattr(y, "dtype") or hasattr(y, "type")
is_scalar_type = is_scalar(y)

Expand All @@ -66,19 +63,7 @@ def fill_zeros(result, x, y, name, fill):
shape = result.shape
result = result.astype("float64", copy=False).ravel()

np.putmask(result, mask, fill)

# if we have a fill of inf, then sign it correctly
# (GH#6178 and GH#9308)
if np.isinf(fill):
signs = y if name.startswith(("r", "__r")) else x
signs = np.sign(signs.astype("float", copy=False))
negative_inf_mask = (signs.ravel() < 0) & mask
np.putmask(result, negative_inf_mask, -fill)

if "floordiv" in name: # (GH#9308)
nan_mask = ((y == 0) & (x == 0)).ravel()
np.putmask(result, nan_mask, np.nan)
np.putmask(result, mask, np.nan)

result = result.reshape(shape)

Expand Down Expand Up @@ -172,12 +157,12 @@ def dispatch_fill_zeros(op, left, right, result):
if op is divmod:
result = (
mask_zero_div_zero(left, right, result[0]),
fill_zeros(result[1], left, right, "__mod__", np.nan),
fill_zeros(result[1], left, right),
)
elif op is rdivmod:
result = (
mask_zero_div_zero(right, left, result[0]),
fill_zeros(result[1], left, right, "__rmod__", np.nan),
fill_zeros(result[1], right, left),
)
elif op is operator.floordiv:
# Note: no need to do this for truediv; in py3 numpy behaves the way
Expand All @@ -188,7 +173,7 @@ def dispatch_fill_zeros(op, left, right, result):
# we want.
result = mask_zero_div_zero(right, left, result)
elif op is operator.mod:
result = fill_zeros(result, left, right, "__mod__", np.nan)
result = fill_zeros(result, left, right)
elif op is rmod:
result = fill_zeros(result, left, right, "__rmod__", np.nan)
result = fill_zeros(result, right, left)
return result