Skip to content

ENH: Add numba engine to groupby.min/max #45428

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 5 commits into from
Jan 22, 2022
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Other enhancements
- :class:`StringArray` now accepts array-likes containing nan-likes (``None``, ``np.nan``) for the ``values`` parameter in its constructor in addition to strings and :attr:`pandas.NA`. (:issue:`40839`)
- Improved the rendering of ``categories`` in :class:`CategoricalIndex` (:issue:`45218`)
- :meth:`to_numeric` now preserves float64 arrays when downcasting would generate values not representable in float32 (:issue:`43693`)
- :meth:`.GroupBy.min` and :meth:`.GroupBy.max` now supports `Numba <https://numba.pydata.org/>`_ execution with the ``engine`` keyword (:issue:`45428`)
-

.. ---------------------------------------------------------------------------
Expand Down
48 changes: 40 additions & 8 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2208,17 +2208,49 @@ def prod(

@final
@doc(_groupby_agg_method_template, fname="min", no=False, mc=-1)
def min(self, numeric_only: bool = False, min_count: int = -1):
return self._agg_general(
numeric_only=numeric_only, min_count=min_count, alias="min", npfunc=np.min
)
def min(
self,
numeric_only: bool = False,
min_count: int = -1,
engine: str | None = None,
engine_kwargs: dict[str, bool] | None = None,
):
if maybe_use_numba(engine):
from pandas.core._numba.kernels import sliding_min_max

return self._numba_agg_general(
sliding_min_max, engine_kwargs, "groupby_min", False
)
else:
return self._agg_general(
numeric_only=numeric_only,
min_count=min_count,
alias="min",
npfunc=np.min,
)

@final
@doc(_groupby_agg_method_template, fname="max", no=False, mc=-1)
def max(self, numeric_only: bool = False, min_count: int = -1):
return self._agg_general(
numeric_only=numeric_only, min_count=min_count, alias="max", npfunc=np.max
)
def max(
self,
numeric_only: bool = False,
min_count: int = -1,
engine: str | None = None,
engine_kwargs: dict[str, bool] | None = None,
):
if maybe_use_numba(engine):
from pandas.core._numba.kernels import sliding_min_max

return self._numba_agg_general(
sliding_min_max, engine_kwargs, "groupby_max", True
)
else:
return self._agg_general(
numeric_only=numeric_only,
min_count=min_count,
alias="max",
npfunc=np.max,
)

@final
@doc(_groupby_agg_method_template, fname="first", no=False, mc=-1)
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/groupby/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ def nopython(request):
("std", {"ddof": 1}),
("std", {"ddof": 0}),
("sum", {}),
]
("min", {}),
("max", {}),
],
ids=["mean", "var_1", "var_0", "std_1", "std_0", "sum", "min", "max"],
)
def numba_supported_reductions(request):
"""reductions supported with engine='numba'"""
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/groupby/test_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_cython_vs_numba_frame(
)
expected = getattr(gb, func)(**kwargs)
# check_dtype can be removed if GH 44952 is addressed
check_dtype = func != "sum"
check_dtype = func not in ("sum", "min", "max")
tm.assert_frame_equal(result, expected, check_dtype=check_dtype)

def test_cython_vs_numba_getitem(
Expand All @@ -40,7 +40,7 @@ def test_cython_vs_numba_getitem(
)
expected = getattr(gb, func)(**kwargs)
# check_dtype can be removed if GH 44952 is addressed
check_dtype = func != "sum"
check_dtype = func not in ("sum", "min", "max")
tm.assert_series_equal(result, expected, check_dtype=check_dtype)

def test_cython_vs_numba_series(
Expand All @@ -55,7 +55,7 @@ def test_cython_vs_numba_series(
)
expected = getattr(gb, func)(**kwargs)
# check_dtype can be removed if GH 44952 is addressed
check_dtype = func != "sum"
check_dtype = func not in ("sum", "min", "max")
tm.assert_series_equal(result, expected, check_dtype=check_dtype)

def test_as_index_false_unsupported(self, numba_supported_reductions):
Expand Down