diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index df2907bf591dd..5738dd9e4a128 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -43,6 +43,7 @@ ABCSeries, ABCSparseArray, ABCSparseSeries, + ABCTimedeltaArray, ) from pandas.core.dtypes.missing import isna, notna @@ -1709,10 +1710,30 @@ 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 + 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