Skip to content

REF: consolidate numeric_only checks in GroupBy #51185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
is_dict_like,
is_integer_dtype,
is_interval_dtype,
is_numeric_dtype,
is_scalar,
)
from pandas.core.dtypes.missing import (
Expand Down Expand Up @@ -172,9 +173,18 @@ def _wrap_agged_manager(self, mgr: Manager) -> Series:
# NB: caller is responsible for setting ser.index
return ser

def _get_data_to_aggregate(self) -> SingleManager:
def _get_data_to_aggregate(
self, *, numeric_only: bool = False, name: str | None = None
) -> SingleManager:
ser = self._selected_obj
single = ser._mgr
if numeric_only and not is_numeric_dtype(ser.dtype):
# GH#41291 match Series behavior
kwd_name = "numeric_only"
raise TypeError(
f"Cannot use {kwd_name}=True with "
f"{type(self).__name__}.{name} and non-numeric dtypes."
)
return single

def _iterate_slices(self) -> Iterable[Series]:
Expand Down Expand Up @@ -1542,9 +1552,9 @@ def _cython_transform(
# test_transform_numeric_ret
# With self.axis == 1, _get_data_to_aggregate does a transpose
# so we always have a single block.
mgr: Manager2D = self._get_data_to_aggregate()
if numeric_only:
mgr = mgr.get_numeric_data(copy=False)
mgr: Manager2D = self._get_data_to_aggregate(
numeric_only=numeric_only, name=how
)

def arr_func(bvalues: ArrayLike) -> ArrayLike:
return self.grouper._cython_operation(
Expand Down Expand Up @@ -1864,12 +1874,18 @@ def _gotitem(self, key, ndim: int, subset=None):

raise AssertionError("invalid ndim for _gotitem")

def _get_data_to_aggregate(self) -> Manager2D:
def _get_data_to_aggregate(
self, *, numeric_only: bool = False, name: str | None = None
) -> Manager2D:
obj = self._obj_with_exclusions
if self.axis == 1:
return obj.T._mgr
mgr = obj.T._mgr
else:
return obj._mgr
mgr = obj._mgr

if numeric_only:
mgr = mgr.get_numeric_data(copy=False)
return mgr

def _indexed_output_to_ndframe(
self, output: Mapping[base.OutputKey, ArrayLike]
Expand Down
37 changes: 3 additions & 34 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1540,22 +1540,9 @@ def _cython_agg_general(
# Note: we never get here with how="ohlc" for DataFrameGroupBy;
# that goes through SeriesGroupBy

data = self._get_data_to_aggregate()
data = self._get_data_to_aggregate(numeric_only=numeric_only, name=how)
is_ser = data.ndim == 1

if numeric_only:
if is_ser and not is_numeric_dtype(self._selected_obj.dtype):
# GH#41291 match Series behavior
kwd_name = "numeric_only"
if how in ["any", "all"]:
kwd_name = "bool_only"
raise TypeError(
f"Cannot use {kwd_name}={numeric_only} with "
f"{type(self).__name__}.{how} and non-numeric types."
)
if not is_ser:
data = data.get_numeric_data(copy=False)

def array_func(values: ArrayLike) -> ArrayLike:
try:
result = self.grouper._cython_operation(
Expand Down Expand Up @@ -2034,15 +2021,6 @@ def std(

return np.sqrt(self._numba_agg_general(sliding_var, engine_kwargs, ddof))
else:
if (
numeric_only
and self.obj.ndim == 1
and not is_numeric_dtype(self.obj.dtype)
):
raise TypeError(
f"{type(self).__name__}.std called with "
f"numeric_only={numeric_only} and dtype {self.obj.dtype}"
)

def _preprocessing(values):
if isinstance(values, BaseMaskedArray):
Expand Down Expand Up @@ -3114,11 +3092,6 @@ def quantile(
a 2.0
b 3.0
"""
if numeric_only and self.obj.ndim == 1 and not is_numeric_dtype(self.obj.dtype):
raise TypeError(
f"{type(self).__name__}.quantile called with "
f"numeric_only={numeric_only} and dtype {self.obj.dtype}"
)

def pre_processor(vals: ArrayLike) -> tuple[np.ndarray, DtypeObj | None]:
if is_object_dtype(vals):
Expand Down Expand Up @@ -3258,8 +3231,7 @@ def blk_func(values: ArrayLike) -> ArrayLike:

obj = self._obj_with_exclusions
is_ser = obj.ndim == 1
mgr = self._get_data_to_aggregate()
data = mgr.get_numeric_data() if numeric_only else mgr
data = self._get_data_to_aggregate(numeric_only=numeric_only, name="quantile")
res_mgr = data.grouped_reduce(blk_func)

if is_ser:
Expand Down Expand Up @@ -3716,10 +3688,7 @@ def blk_func(values: ArrayLike) -> ArrayLike:

# Operate block-wise instead of column-by-column
is_ser = obj.ndim == 1
mgr = self._get_data_to_aggregate()

if numeric_only:
mgr = mgr.get_numeric_data()
mgr = self._get_data_to_aggregate(numeric_only=numeric_only, name=how)

res_mgr = mgr.grouped_reduce(blk_func)

Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/groupby/aggregate/test_cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def test_cython_agg_boolean():
def test_cython_agg_nothing_to_agg():
frame = DataFrame({"a": np.random.randint(0, 5, 50), "b": ["foo", "bar"] * 25})

with pytest.raises(TypeError, match="Cannot use numeric_only=True"):
msg = "Cannot use numeric_only=True with SeriesGroupBy.mean and non-numeric dtypes"
with pytest.raises(TypeError, match=msg):
frame.groupby("a")["b"].mean(numeric_only=True)

with pytest.raises(TypeError, match="Could not convert (foo|bar)*"):
Expand All @@ -117,7 +118,8 @@ def test_cython_agg_nothing_to_agg_with_dates():
"dates": pd.date_range("now", periods=50, freq="T"),
}
)
with pytest.raises(TypeError, match="Cannot use numeric_only=True"):
msg = "Cannot use numeric_only=True with SeriesGroupBy.mean and non-numeric dtypes"
with pytest.raises(TypeError, match=msg):
frame.groupby("b").dates.mean(numeric_only=True)


Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,11 +1555,10 @@ def test_deprecate_numeric_only_series(dtype, groupby_func, request):
elif dtype is object:
msg = "|".join(
[
"Cannot use numeric_only=True",
"called with numeric_only=True and dtype object",
"SeriesGroupBy.sem called with numeric_only=True and dtype object",
"Series.skew does not allow numeric_only=True with non-numeric",
"got an unexpected keyword argument 'numeric_only'",
"is not supported for object dtype",
"cum(sum|prod|min|max) is not supported for object dtype",
r"Cannot use numeric_only=True with SeriesGroupBy\..* and non-numeric",
]
)
with pytest.raises(TypeError, match=msg):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,8 @@ def test_series_downsample_method(method, numeric_only, expected_data):

func = getattr(resampled, method)
if numeric_only and numeric_only is not lib.no_default:
with pytest.raises(TypeError, match="Cannot use numeric_only=True"):
msg = rf"Cannot use numeric_only=True with SeriesGroupBy\.{method}"
with pytest.raises(TypeError, match=msg):
func(**kwargs)
elif method == "prod":
with pytest.raises(TypeError, match="can't multiply sequence by non-int"):
Expand Down