Skip to content

REF: Avoid dispatching Series ops to pd.Index #27268

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 4 commits into from
Jul 8, 2019
Merged
Changes from 1 commit
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
31 changes: 27 additions & 4 deletions pandas/core/ops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
ABCSeries,
ABCSparseArray,
ABCSparseSeries,
ABCTimedeltaArray,
)
from pandas.core.dtypes.missing import isna, notna

Expand Down Expand Up @@ -1716,10 +1717,32 @@ def wrapper(left, right):
# Note: we cannot use dispatch_to_index_op because
# that may incorrectly raise TypeError when we
# should get NullFrequencyError
result = op(pd.Index(left), right)
return construct_result(
left, result, index=left.index, name=res_name, dtype=result.dtype
)
orig_right = right
if is_scalar(right):
# broadcast and wrap in a TimedeltaIndex
# TODO: possible perf improvement by using NaTD scalar
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this this is a worthwhile comment here as NaTD is not likely to happen

Copy link
Member Author

Choose a reason for hiding this comment

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

I think there's a decent chance we'll implement it for internal use to de-duplicate logic like this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed comment

# here (GH#24983)
assert np.isnat(right)
right = np.broadcast_to(right, left.shape)
right = pd.TimedeltaIndex(right)

assert isinstance(right, (pd.TimedeltaIndex, ABCTimedeltaArray, ABCSeries))
try:
result = op(left._values, right)
except NullFrequencyError:
if orig_right is not right:
# i.e. scalar timedelta64('NaT')
# We get a NullFrequencyError because we broadcast to
# TimedeltaIndex, but this should be TypeError.
raise TypeError(
"incompatible type for a datetime/timedelta "
"operation [{name}]".format(name=op.__name__)
)
raise

# We do not pass dtype to ensure that the Series constructor
# does inference in the case where `result` has object-dtype.
return construct_result(left, result, index=left.index, name=res_name)

lvalues = left.values
rvalues = right
Expand Down