Skip to content

Commit 52e07b5

Browse files
committed
BUG: Validate numeric_only parameter in groupby aggregations
1 parent f2eb667 commit 52e07b5

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

pandas/core/groupby/groupby.py

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,44 +1754,48 @@ def _cython_agg_general(
17541754
**kwargs,
17551755
):
17561756
# Note: we never get here with how="ohlc" for DataFrameGroupBy;
1757-
# that goes through SeriesGroupBy
1757+
# that goes through SeriesGroupBy
17581758

1759-
data = self._get_data_to_aggregate(numeric_only=numeric_only, name=how)
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)
17601762

1761-
def array_func(values: ArrayLike) -> ArrayLike:
1762-
try:
1763-
result = self._grouper._cython_operation(
1764-
"aggregate",
1765-
values,
1766-
how,
1767-
axis=data.ndim - 1,
1768-
min_count=min_count,
1769-
**kwargs,
1770-
)
1771-
except NotImplementedError:
1772-
# generally if we have numeric_only=False
1773-
# and non-applicable functions
1774-
# try to python agg
1775-
# TODO: shouldn't min_count matter?
1776-
# TODO: avoid special casing SparseArray here
1777-
if how in ["any", "all"] and isinstance(values, SparseArray):
1778-
pass
1779-
elif alt is None or how in ["any", "all", "std", "sem"]:
1780-
raise # TODO: re-raise as TypeError? should not be reached
1781-
else:
1782-
return result
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
17831785

1784-
assert alt is not None
1785-
result = self._agg_py_fallback(how, values, ndim=data.ndim, alt=alt)
1786-
return result
1786+
assert alt is not None
1787+
result = self._agg_py_fallback(how, values, ndim=data.ndim, alt=alt)
1788+
return result
17871789

1788-
new_mgr = data.grouped_reduce(array_func)
1789-
res = self._wrap_agged_manager(new_mgr)
1790-
if how in ["idxmin", "idxmax"]:
1791-
# mypy expects how to be Literal["idxmin", "idxmax"].
1792-
res = self._wrap_idxmax_idxmin(res, how=how, skipna=kwargs["skipna"]) # type: ignore[arg-type]
1793-
out = self._wrap_aggregated_output(res)
1794-
return out
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")
17951799

17961800
def _cython_transform(self, how: str, numeric_only: bool = False, **kwargs):
17971801
raise AbstractMethodError(self)

0 commit comments

Comments
 (0)