@@ -1933,9 +1933,7 @@ def check_for_ordered(self, op) -> None:
19331933 """assert that we are ordered"""
19341934 if not self .ordered :
19351935 raise TypeError (
1936- f"Categorical is not ordered for operation { op } \n "
1937- "you can use .as_ordered() to change the "
1938- "Categorical to an ordered one\n "
1936+ f"operation '{ op } ' is not supported for dtype '{ self .dtype } '"
19391937 )
19401938
19411939 def argsort (
@@ -2419,9 +2417,17 @@ def _reverse_indexer(self) -> dict[Hashable, npt.NDArray[np.intp]]:
24192417 # ------------------------------------------------------------------
24202418 # Reductions
24212419
2420+ def _supports_reduction (self , op_name : str ) -> bool :
2421+ return op_name in {"min" , "max" , "mode" }
2422+
24222423 def _reduce (
24232424 self , name : str , * , skipna : bool = True , keepdims : bool = False , ** kwargs
24242425 ):
2426+ if not self ._supports_reduction (name ):
2427+ raise TypeError (
2428+ f"operation '{ name } ' is not supported for dtype '{ self .dtype } '"
2429+ )
2430+
24252431 result = super ()._reduce (name , skipna = skipna , keepdims = keepdims , ** kwargs )
24262432 if name in ["argmax" , "argmin" ]:
24272433 # don't wrap in Categorical!
@@ -2566,7 +2572,9 @@ def _accumulate(self, name: str, skipna: bool = True, **kwargs) -> Self:
25662572 elif name == "cummax" :
25672573 func = np .maximum .accumulate
25682574 else :
2569- raise TypeError (f"Accumulation { name } not supported for { type (self )} " )
2575+ raise TypeError (
2576+ f"operation '{ name } ' is not supported for dtype '{ self .dtype } '"
2577+ )
25702578 self .check_for_ordered (name )
25712579
25722580 codes = self .codes .copy ()
@@ -2766,12 +2774,12 @@ def _groupby_op(
27662774
27672775 dtype = self .dtype
27682776 if how in ["sum" , "prod" , "cumsum" , "cumprod" , "skew" , "kurt" ]:
2769- raise TypeError (f"{ dtype } type does not support { how } operations " )
2777+ raise TypeError (f"operation ' { how } ' is not supported for dtype ' { dtype } ' " )
27702778 if how in ["min" , "max" , "rank" , "idxmin" , "idxmax" ] and not dtype .ordered :
27712779 # raise TypeError instead of NotImplementedError to ensure we
27722780 # don't go down a group-by-group path, since in the empty-groups
27732781 # case that would fail to raise
2774- raise TypeError (f"Cannot perform { how } with non-ordered Categorical " )
2782+ raise TypeError (f"operation ' { how } ' is not supported for dtype ' { dtype } ' " )
27752783 if how not in [
27762784 "rank" ,
27772785 "any" ,
@@ -2784,8 +2792,10 @@ def _groupby_op(
27842792 "idxmax" ,
27852793 ]:
27862794 if kind == "transform" :
2787- raise TypeError (f"{ dtype } type does not support { how } operations" )
2788- raise TypeError (f"{ dtype } dtype does not support aggregation '{ how } '" )
2795+ raise TypeError (
2796+ f"operation '{ how } ' is not supported for dtype '{ dtype } '"
2797+ )
2798+ raise TypeError (f"operation '{ how } ' is not supported for dtype '{ dtype } '" )
27892799
27902800 result_mask = None
27912801 mask = self .isna ()
0 commit comments