37
37
from pandas .core .dtypes .generic import (
38
38
ABCDataFrame ,
39
39
ABCDatetimeArray ,
40
+ ABCDatetimeIndex ,
40
41
ABCIndex ,
41
42
ABCIndexClass ,
42
43
ABCSeries ,
43
44
ABCSparseArray ,
44
45
ABCSparseSeries ,
45
46
ABCTimedeltaArray ,
47
+ ABCTimedeltaIndex ,
46
48
)
47
49
from pandas .core .dtypes .missing import isna , notna
48
50
@@ -92,7 +94,7 @@ def get_op_result_name(left, right):
92
94
name : object
93
95
Usually a string
94
96
"""
95
- # `left` is always a pd. Series when called from within ops
97
+ # `left` is always a Series when called from within ops
96
98
if isinstance (right , (ABCSeries , ABCIndexClass )):
97
99
name = _maybe_match_name (left , right )
98
100
else :
@@ -602,13 +604,16 @@ def dispatch_to_extension_op(op, left, right):
602
604
else :
603
605
new_right = right
604
606
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
612
617
613
618
614
619
# -----------------------------------------------------------------------------
@@ -942,7 +947,8 @@ def wrapper(left, right):
942
947
is_extension_array_dtype (right ) and not is_scalar (right )
943
948
):
944
949
# 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 )
946
952
947
953
elif is_timedelta64_dtype (left ):
948
954
result = dispatch_to_index_op (op , left , right , pd .TimedeltaIndex )
@@ -961,7 +967,7 @@ def wrapper(left, right):
961
967
right = np .broadcast_to (right , left .shape )
962
968
right = pd .TimedeltaIndex (right )
963
969
964
- assert isinstance (right , (pd . TimedeltaIndex , ABCTimedeltaArray , ABCSeries ))
970
+ assert isinstance (right , (ABCTimedeltaIndex , ABCTimedeltaArray , ABCSeries ))
965
971
try :
966
972
result = op (left ._values , right )
967
973
except NullFrequencyError :
@@ -979,7 +985,7 @@ def wrapper(left, right):
979
985
# does inference in the case where `result` has object-dtype.
980
986
return construct_result (left , result , index = left .index , name = res_name )
981
987
982
- elif isinstance (right , (ABCDatetimeArray , pd . DatetimeIndex )):
988
+ elif isinstance (right , (ABCDatetimeArray , ABCDatetimeIndex )):
983
989
result = op (left ._values , right )
984
990
return construct_result (left , result , index = left .index , name = res_name )
985
991
@@ -1086,7 +1092,7 @@ def wrapper(self, other, axis=None):
1086
1092
raise ValueError ("Can only compare identically-labeled Series objects" )
1087
1093
1088
1094
elif is_categorical_dtype (self ):
1089
- # Dispatch to Categorical implementation; pd. CategoricalIndex
1095
+ # Dispatch to Categorical implementation; CategoricalIndex
1090
1096
# behavior is non-canonical GH#19513
1091
1097
res_values = dispatch_to_index_op (op , self , other , pd .Categorical )
1092
1098
return self ._constructor (res_values , index = self .index , name = res_name )
@@ -1107,7 +1113,10 @@ def wrapper(self, other, axis=None):
1107
1113
):
1108
1114
# Note: the `not is_scalar(other)` condition rules out
1109
1115
# 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 )
1111
1120
1112
1121
elif isinstance (other , ABCSeries ):
1113
1122
# By this point we have checked that self._indexed_same(other)
0 commit comments