@@ -86,6 +86,7 @@ class providing the base-class of operations.
8686 is_object_dtype ,
8787 is_scalar ,
8888 is_string_dtype ,
89+ is_bool ,
8990 needs_i8_conversion ,
9091 pandas_dtype ,
9192)
@@ -1756,46 +1757,45 @@ def _cython_agg_general(
17561757 # Note: we never get here with how="ohlc" for DataFrameGroupBy;
17571758 # that goes through SeriesGroupBy
17581759
1759- # Check to confirm numeric_only is fed either True or False and no other data type
1760- if (isinstance (numeric_only , bool )):
1761- data = self ._get_data_to_aggregate (numeric_only = numeric_only , name = how )
1760+ if not is_bool (numeric_only ):
1761+ raise ValueError ("numeric_only accepts only Boolean values" )
17621762
1763- def array_func (values : ArrayLike ) -> ArrayLike :
1764- try :
1765- result = self ._grouper ._cython_operation (
1766- "aggregate" ,
1767- values ,
1768- how ,
1769- axis = data .ndim - 1 ,
1770- min_count = min_count ,
1771- ** kwargs ,
1772- )
1773- except NotImplementedError :
1774- # generally if we have numeric_only=False
1775- # and non-applicable functions
1776- # try to python agg
1777- # TODO: shouldn't min_count matter?
1778- # TODO: avoid special casing SparseArray here
1779- if how in ["any" , "all" ] and isinstance (values , SparseArray ):
1780- pass
1781- elif alt is None or how in ["any" , "all" , "std" , "sem" ]:
1782- raise # TODO: re-raise as TypeError? should not be reached
1783- else :
1784- return result
1763+ data = self ._get_data_to_aggregate (numeric_only = numeric_only , name = how )
17851764
1786- assert alt is not None
1787- result = self ._agg_py_fallback (how , values , ndim = data .ndim , alt = alt )
1765+ def array_func (values : ArrayLike ) -> ArrayLike :
1766+ try :
1767+ result = self ._grouper ._cython_operation (
1768+ "aggregate" ,
1769+ values ,
1770+ how ,
1771+ axis = data .ndim - 1 ,
1772+ min_count = min_count ,
1773+ ** kwargs ,
1774+ )
1775+ except NotImplementedError :
1776+ # generally if we have numeric_only=False
1777+ # and non-applicable functions
1778+ # try to python agg
1779+ # TODO: shouldn't min_count matter?
1780+ # TODO: avoid special casing SparseArray here
1781+ if how in ["any" , "all" ] and isinstance (values , SparseArray ):
1782+ pass
1783+ elif alt is None or how in ["any" , "all" , "std" , "sem" ]:
1784+ raise # TODO: re-raise as TypeError? should not be reached
1785+ else :
17881786 return result
17891787
1790- new_mgr = data .grouped_reduce (array_func )
1791- res = self ._wrap_agged_manager (new_mgr )
1792- if how in ["idxmin" , "idxmax" ]:
1793- # mypy expects how to be Literal["idxmin", "idxmax"].
1794- res = self ._wrap_idxmax_idxmin (res , how = how , skipna = kwargs ["skipna" ]) # type: ignore[arg-type]
1795- out = self ._wrap_aggregated_output (res )
1796- return out
1797- else :
1798- raise ValueError ("numeric_only accepts only Boolean values" )
1788+ assert alt is not None
1789+ result = self ._agg_py_fallback (how , values , ndim = data .ndim , alt = alt )
1790+ return result
1791+
1792+ new_mgr = data .grouped_reduce (array_func )
1793+ res = self ._wrap_agged_manager (new_mgr )
1794+ if how in ["idxmin" , "idxmax" ]:
1795+ # mypy expects how to be Literal["idxmin", "idxmax"].
1796+ res = self ._wrap_idxmax_idxmin (res , how = how , skipna = kwargs ["skipna" ]) # type: ignore[arg-type]
1797+ out = self ._wrap_aggregated_output (res )
1798+ return out
17991799
18001800 def _cython_transform (self , how : str , numeric_only : bool = False , ** kwargs ):
18011801 raise AbstractMethodError (self )
0 commit comments