Skip to content

Commit cb3c3c6

Browse files
committed
REF: make dispatch_to_extension_op signature match dispatch_to_index_op
1 parent 7ef3308 commit cb3c3c6

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

pandas/core/ops/__init__.py

+22-13
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@
3737
from pandas.core.dtypes.generic import (
3838
ABCDataFrame,
3939
ABCDatetimeArray,
40+
ABCDatetimeIndex,
4041
ABCIndex,
4142
ABCIndexClass,
4243
ABCSeries,
4344
ABCSparseArray,
4445
ABCSparseSeries,
4546
ABCTimedeltaArray,
47+
ABCTimedeltaIndex,
4648
)
4749
from pandas.core.dtypes.missing import isna, notna
4850

@@ -92,7 +94,7 @@ def get_op_result_name(left, right):
9294
name : object
9395
Usually a string
9496
"""
95-
# `left` is always a pd.Series when called from within ops
97+
# `left` is always a Series when called from within ops
9698
if isinstance(right, (ABCSeries, ABCIndexClass)):
9799
name = _maybe_match_name(left, right)
98100
else:
@@ -602,13 +604,16 @@ def dispatch_to_extension_op(op, left, right):
602604
else:
603605
new_right = right
604606

605-
res_values = op(new_left, new_right)
606-
res_name = get_op_result_name(left, right)
607-
608-
if op.__name__ in ["divmod", "rdivmod"]:
609-
return _construct_divmod_result(left, res_values, left.index, res_name)
610-
611-
return _construct_result(left, res_values, left.index, res_name)
607+
try:
608+
res_values = op(new_left, new_right)
609+
except NullFrequencyError:
610+
# DatetimeIndex and TimedeltaIndex with freq == None raise ValueError
611+
# on add/sub of integers (or int-like). We re-raise as a TypeError.
612+
raise TypeError(
613+
"incompatible type for a datetime/timedelta "
614+
"operation [{name}]".format(name=op.__name__)
615+
)
616+
return res_values
612617

613618

614619
# -----------------------------------------------------------------------------
@@ -942,7 +947,8 @@ def wrapper(left, right):
942947
is_extension_array_dtype(right) and not is_scalar(right)
943948
):
944949
# GH#22378 disallow scalar to exclude e.g. "category", "Int64"
945-
return dispatch_to_extension_op(op, left, right)
950+
result = dispatch_to_extension_op(op, left, right)
951+
return construct_result(left, result, index=left.index, name=res_name)
946952

947953
elif is_timedelta64_dtype(left):
948954
result = dispatch_to_index_op(op, left, right, pd.TimedeltaIndex)
@@ -961,7 +967,7 @@ def wrapper(left, right):
961967
right = np.broadcast_to(right, left.shape)
962968
right = pd.TimedeltaIndex(right)
963969

964-
assert isinstance(right, (pd.TimedeltaIndex, ABCTimedeltaArray, ABCSeries))
970+
assert isinstance(right, (ABCTimedeltaIndex, ABCTimedeltaArray, ABCSeries))
965971
try:
966972
result = op(left._values, right)
967973
except NullFrequencyError:
@@ -979,7 +985,7 @@ def wrapper(left, right):
979985
# does inference in the case where `result` has object-dtype.
980986
return construct_result(left, result, index=left.index, name=res_name)
981987

982-
elif isinstance(right, (ABCDatetimeArray, pd.DatetimeIndex)):
988+
elif isinstance(right, (ABCDatetimeArray, ABCDatetimeIndex)):
983989
result = op(left._values, right)
984990
return construct_result(left, result, index=left.index, name=res_name)
985991

@@ -1086,7 +1092,7 @@ def wrapper(self, other, axis=None):
10861092
raise ValueError("Can only compare identically-labeled Series objects")
10871093

10881094
elif is_categorical_dtype(self):
1089-
# Dispatch to Categorical implementation; pd.CategoricalIndex
1095+
# Dispatch to Categorical implementation; CategoricalIndex
10901096
# behavior is non-canonical GH#19513
10911097
res_values = dispatch_to_index_op(op, self, other, pd.Categorical)
10921098
return self._constructor(res_values, index=self.index, name=res_name)
@@ -1107,7 +1113,10 @@ def wrapper(self, other, axis=None):
11071113
):
11081114
# Note: the `not is_scalar(other)` condition rules out
11091115
# e.g. other == "category"
1110-
return dispatch_to_extension_op(op, self, other)
1116+
res_values = dispatch_to_extension_op(op, self, other)
1117+
return self._constructor(
1118+
res_values, index=self.index, name=res_name
1119+
).rename(res_name)
11111120

11121121
elif isinstance(other, ABCSeries):
11131122
# By this point we have checked that self._indexed_same(other)

0 commit comments

Comments
 (0)