@@ -550,7 +550,8 @@ def _validate_comparison_value(self, other):
550550 raise InvalidComparison (other )
551551
552552 elif len (other ) != len (self ):
553- raise ValueError ("Lengths must match" )
553+ msg = ops .get_shape_exception_message (self , other )
554+ raise ValueError (msg )
554555
555556 else :
556557 try :
@@ -963,6 +964,12 @@ def _is_unique(self) -> bool:
963964 # ------------------------------------------------------------------
964965 # Arithmetic Methods
965966
967+ def _supports_scalar_op (self , other , op_name ):
968+ return True
969+
970+ def _supports_array_op (self , other , op_name ):
971+ return True
972+
966973 def _cmp_method (self , other , op ):
967974 if self .ndim > 1 and getattr (other , "shape" , None ) == self .shape :
968975 # TODO: handle 2D-like listlikes
@@ -1099,9 +1106,8 @@ def _get_arithmetic_result_freq(self, other) -> BaseOffset | None:
10991106 @final
11001107 def _add_datetimelike_scalar (self , other ) -> DatetimeArray :
11011108 if not lib .is_np_dtype (self .dtype , "m" ):
1102- raise TypeError (
1103- f"cannot add { type (self ).__name__ } and { type (other ).__name__ } "
1104- )
1109+ msg = ops .get_op_exception_message (self , other )
1110+ raise TypeError (msg )
11051111
11061112 self = cast ("TimedeltaArray" , self )
11071113
@@ -1133,9 +1139,8 @@ def _add_datetimelike_scalar(self, other) -> DatetimeArray:
11331139 @final
11341140 def _add_datetime_arraylike (self , other : DatetimeArray ) -> DatetimeArray :
11351141 if not lib .is_np_dtype (self .dtype , "m" ):
1136- raise TypeError (
1137- f"cannot add { type (self ).__name__ } and { type (other ).__name__ } "
1138- )
1142+ msg = ops .get_op_exception_message (self , other )
1143+ raise TypeError (msg )
11391144
11401145 # defer to DatetimeArray.__add__
11411146 return other + self
@@ -1145,7 +1150,8 @@ def _sub_datetimelike_scalar(
11451150 self , other : datetime | np .datetime64
11461151 ) -> TimedeltaArray :
11471152 if self .dtype .kind != "M" :
1148- raise TypeError (f"cannot subtract a datelike from a { type (self ).__name__ } " )
1153+ msg = ops .get_op_exception_message (self , other )
1154+ raise TypeError (msg )
11491155
11501156 self = cast ("DatetimeArray" , self )
11511157 # subtract a datetime from myself, yielding a ndarray[timedelta64[ns]]
@@ -1162,10 +1168,8 @@ def _sub_datetimelike_scalar(
11621168 @final
11631169 def _sub_datetime_arraylike (self , other : DatetimeArray ) -> TimedeltaArray :
11641170 if self .dtype .kind != "M" :
1165- raise TypeError (f"cannot subtract a datelike from a { type (self ).__name__ } " )
1166-
1167- if len (self ) != len (other ):
1168- raise ValueError ("cannot add indices of unequal length" )
1171+ msg = ops .get_op_exception_message (self , other )
1172+ raise TypeError (msg )
11691173
11701174 self = cast ("DatetimeArray" , self )
11711175
@@ -1195,7 +1199,8 @@ def _sub_datetimelike(self, other: Timestamp | DatetimeArray) -> TimedeltaArray:
11951199 @final
11961200 def _add_period (self , other : Period ) -> PeriodArray :
11971201 if not lib .is_np_dtype (self .dtype , "m" ):
1198- raise TypeError (f"cannot add Period to a { type (self ).__name__ } " )
1202+ msg = ops .get_op_exception_message (self , other )
1203+ raise TypeError (msg )
11991204
12001205 # We will wrap in a PeriodArray and defer to the reversed operation
12011206 from pandas .core .arrays .period import PeriodArray
@@ -1239,7 +1244,8 @@ def _add_timedelta_arraylike(self, other: TimedeltaArray) -> Self:
12391244 # overridden by PeriodArray
12401245
12411246 if len (self ) != len (other ):
1242- raise ValueError ("cannot add indices of unequal length" )
1247+ msg = ops .get_shape_exception_message (self , other )
1248+ raise ValueError (msg )
12431249
12441250 self , other = cast (
12451251 "DatetimeArray | TimedeltaArray" , self
@@ -1267,9 +1273,8 @@ def _add_nat(self) -> Self:
12671273 Add pd.NaT to self
12681274 """
12691275 if isinstance (self .dtype , PeriodDtype ):
1270- raise TypeError (
1271- f"Cannot add { type (self ).__name__ } and { type (NaT ).__name__ } "
1272- )
1276+ msg = ops .get_op_exception_message (self , NaT )
1277+ raise TypeError (msg )
12731278
12741279 # GH#19124 pd.NaT is treated like a timedelta for both timedelta
12751280 # and datetime dtypes
@@ -1308,9 +1313,8 @@ def _sub_periodlike(self, other: Period | PeriodArray) -> npt.NDArray[np.object_
13081313 # If the operation is well-defined, we return an object-dtype ndarray
13091314 # of DateOffsets. Null entries are filled with pd.NaT
13101315 if not isinstance (self .dtype , PeriodDtype ):
1311- raise TypeError (
1312- f"cannot subtract { type (other ).__name__ } from { type (self ).__name__ } "
1313- )
1316+ msg = ops .get_op_exception_message (self , other )
1317+ raise TypeError (msg )
13141318
13151319 self = cast ("PeriodArray" , self )
13161320 self ._check_compatible_with (other )
@@ -1519,13 +1523,13 @@ def __rsub__(self, other):
15191523 elif self .dtype .kind == "M" and hasattr (other , "dtype" ) and not other_is_dt64 :
15201524 # GH#19959 datetime - datetime is well-defined as timedelta,
15211525 # but any other type - datetime is not well-defined.
1522- raise TypeError (
1523- f"cannot subtract { type (self ).__name__ } from "
1524- f"{ type (other ).__name__ } [{ other .dtype } ]"
1525- )
1526+ msg = ops .get_op_exception_message (self , other )
1527+ raise TypeError (msg )
1528+
15261529 elif isinstance (self .dtype , PeriodDtype ) and lib .is_np_dtype (other_dtype , "m" ):
15271530 # TODO: Can we simplify/generalize these cases at all?
1528- raise TypeError (f"cannot subtract { type (self ).__name__ } from { other .dtype } " )
1531+ msg = ops .get_op_exception_message (self , other )
1532+ raise TypeError (msg )
15291533 elif lib .is_np_dtype (self .dtype , "m" ):
15301534 self = cast ("TimedeltaArray" , self )
15311535 return (- self ) + other
0 commit comments