@@ -1058,69 +1058,76 @@ def _bool_method_SERIES(cls, op, special):
1058
1058
Wrapper function for Series arithmetic operations, to avoid
1059
1059
code duplication.
1060
1060
"""
1061
+ name = _get_op_name (op , special )
1061
1062
1062
1063
def na_op (x , y ):
1063
1064
try :
1064
1065
result = op (x , y )
1065
1066
except TypeError :
1066
- if isinstance (y , list ):
1067
- y = construct_1d_object_array_from_listlike (y )
1068
-
1069
- if isinstance (y , (np .ndarray , ABCSeries )):
1070
- if (is_bool_dtype (x .dtype ) and is_bool_dtype (y .dtype )):
1071
- result = op (x , y ) # when would this be hit?
1072
- else :
1073
- x = _ensure_object (x )
1074
- y = _ensure_object (y )
1075
- result = lib .vec_binop (x , y , op )
1067
+ assert not isinstance (y , (list , ABCSeries , pd .Index ))
1068
+ if isinstance (y , np .ndarray ):
1069
+ # This next assertion is here because there used to be
1070
+ # a branch that specifically caught this case.
1071
+ assert not (is_bool_dtype (x ) and is_bool_dtype (y ))
1072
+ x = _ensure_object (x )
1073
+ y = _ensure_object (y )
1074
+ result = lib .vec_binop (x , y , op )
1076
1075
else :
1077
1076
# let null fall thru
1077
+ assert is_scalar (y )
1078
1078
if not isna (y ):
1079
1079
y = bool (y )
1080
1080
try :
1081
1081
result = lib .scalar_binop (x , y , op )
1082
1082
except :
1083
- msg = ("cannot compare a dtyped [{dtype}] array "
1084
- "with a scalar of type [{type }]"
1085
- ) .format (dtype = x .dtype , type = type ( y ). __name__ )
1086
- raise TypeError ( msg )
1083
+ raise TypeError ("cannot compare a dtyped [{dtype}] array "
1084
+ "with a scalar of type [{typ }]"
1085
+ .format (dtype = x .dtype ,
1086
+ typ = type ( y ). __name__ ) )
1087
1087
1088
1088
return result
1089
1089
1090
+ fill_int = lambda x : x .fillna (0 )
1091
+ fill_bool = lambda x : x .fillna (False ).astype (bool )
1092
+
1090
1093
def wrapper (self , other ):
1091
1094
is_self_int_dtype = is_integer_dtype (self .dtype )
1092
1095
1093
- fill_int = lambda x : x .fillna (0 )
1094
- fill_bool = lambda x : x .fillna (False ).astype (bool )
1095
-
1096
1096
self , other = _align_method_SERIES (self , other , align_asobject = True )
1097
1097
1098
+ res_name = _get_series_op_result_name (self , other )
1099
+
1098
1100
if isinstance (other , ABCDataFrame ):
1099
1101
# Defer to DataFrame implementation; fail early
1100
1102
return NotImplemented
1101
1103
1102
- elif isinstance (other , ABCSeries ):
1103
- name = com ._maybe_match_name (self , other )
1104
+ elif isinstance (other , (ABCSeries , pd .Index )):
1104
1105
is_other_int_dtype = is_integer_dtype (other .dtype )
1105
1106
other = fill_int (other ) if is_other_int_dtype else fill_bool (other )
1106
-
1107
- filler = (fill_int if is_self_int_dtype and is_other_int_dtype
1108
- else fill_bool )
1109
-
1110
- res_values = na_op (self .values , other .values )
1111
- unfilled = self ._constructor (res_values ,
1112
- index = self .index , name = name )
1113
- return filler (unfilled )
1107
+ ovalues = other .values
1114
1108
1115
1109
else :
1116
1110
# scalars, list, tuple, np.array
1117
- filler = (fill_int if is_self_int_dtype and
1118
- is_integer_dtype (np .asarray (other )) else fill_bool )
1111
+ is_other_int_dtype = is_integer_dtype (np .asarray (other ))
1112
+ if isinstance (other , list ):
1113
+ # TODO: Can we do this before the is_integer_dtype check?
1114
+ # could the is_integer_dtype check be checking the wrong
1115
+ # thing? e.g. other = [[0, 1], [2, 3], [4, 5]]?
1116
+ other = construct_1d_object_array_from_listlike (other )
1117
+ ovalues = other
1119
1118
1120
- res_values = na_op (self .values , other )
1121
- unfilled = self ._constructor (res_values , index = self .index )
1122
- return filler (unfilled ).__finalize__ (self )
1119
+ filler = (fill_int if is_self_int_dtype and is_other_int_dtype
1120
+ else fill_bool )
1123
1121
1122
+ res_values = na_op (self .values , ovalues )
1123
+ unfilled = self ._constructor (res_values , index = self .index ,
1124
+ name = res_name )
1125
+ result = filler (unfilled )
1126
+ if not isinstance (other , ABCSeries ):
1127
+ result = result .__finalize__ (self )
1128
+ return result
1129
+
1130
+ wrapper .__name__ = name
1124
1131
return wrapper
1125
1132
1126
1133
0 commit comments