Skip to content

ENH: Add table-wise numba rolling to other agg funcions #38995

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 8 commits into from
Jan 7, 2021
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 ci/deps/azure-37-slow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ dependencies:
- xlwt
- moto
- flask
- numba
1 change: 1 addition & 0 deletions ci/deps/azure-38-slow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ dependencies:
- xlwt
- moto
- flask
- numba
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ For example:

:class:`Rolling` and :class:`Expanding` now support a ``method`` argument with a
``'table'`` option that performs the windowing operation over an entire :class:`DataFrame`.
See ref:`window.overview` for performance and functional benefits. (:issue:`15095`)
See ref:`window.overview` for performance and functional benefits. (:issue:`15095`, :issue:`38995`)

.. _whatsnew_130.enhancements.other:

Expand Down
19 changes: 19 additions & 0 deletions pandas/core/window/numba_.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
from typing import Any, Callable, Dict, Optional, Tuple

import numpy as np
Expand Down Expand Up @@ -220,3 +221,21 @@ def roll_table(
return result

return roll_table


# This function will no longer be needed once numba supports
# axis for all np.nan* agg functions
# https://github.com/numba/numba/issues/1269
@functools.lru_cache(maxsize=None)
def generate_manual_numpy_nan_agg_with_axis(nan_func):
numba = import_optional_dependency("numba")

@numba.jit(nopython=True, nogil=True, parallel=True)
def nan_agg_with_axis(table):
result = np.empty(table.shape[1])
for i in numba.prange(table.shape[1]):
partition = table[:, i]
result[i] = nan_func(partition)
return result

return nan_agg_with_axis
56 changes: 26 additions & 30 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
VariableWindowIndexer,
)
from pandas.core.window.numba_ import (
generate_manual_numpy_nan_agg_with_axis,
generate_numba_apply_func,
generate_numba_table_func,
)
Expand Down Expand Up @@ -1378,16 +1379,15 @@ def sum(self, *args, engine=None, engine_kwargs=None, **kwargs):
nv.validate_window_func("sum", args, kwargs)
if maybe_use_numba(engine):
if self.method == "table":
raise NotImplementedError("method='table' is not supported.")
# Once numba supports np.nansum with axis, args will be relevant.
# https://github.com/numba/numba/issues/6610
args = () if self.method == "single" else (0,)
func = generate_manual_numpy_nan_agg_with_axis(np.nansum)
else:
func = np.nansum

return self.apply(
np.nansum,
func,
raw=True,
engine=engine,
engine_kwargs=engine_kwargs,
args=args,
)
window_func = window_aggregations.roll_sum
return self._apply(window_func, name="sum", **kwargs)
Expand Down Expand Up @@ -1424,16 +1424,15 @@ def max(self, *args, engine=None, engine_kwargs=None, **kwargs):
nv.validate_window_func("max", args, kwargs)
if maybe_use_numba(engine):
if self.method == "table":
raise NotImplementedError("method='table' is not supported.")
# Once numba supports np.nanmax with axis, args will be relevant.
# https://github.com/numba/numba/issues/6610
args = () if self.method == "single" else (0,)
func = generate_manual_numpy_nan_agg_with_axis(np.nanmax)
else:
func = np.nanmax

return self.apply(
np.nanmax,
func,
raw=True,
engine=engine,
engine_kwargs=engine_kwargs,
args=args,
)
window_func = window_aggregations.roll_max
return self._apply(window_func, name="max", **kwargs)
Expand Down Expand Up @@ -1496,16 +1495,15 @@ def min(self, *args, engine=None, engine_kwargs=None, **kwargs):
nv.validate_window_func("min", args, kwargs)
if maybe_use_numba(engine):
if self.method == "table":
raise NotImplementedError("method='table' is not supported.")
# Once numba supports np.nanmin with axis, args will be relevant.
# https://github.com/numba/numba/issues/6610
args = () if self.method == "single" else (0,)
func = generate_manual_numpy_nan_agg_with_axis(np.nanmin)
else:
func = np.nanmin

return self.apply(
np.nanmin,
func,
raw=True,
engine=engine,
engine_kwargs=engine_kwargs,
args=args,
)
window_func = window_aggregations.roll_min
return self._apply(window_func, name="min", **kwargs)
Expand All @@ -1514,16 +1512,15 @@ def mean(self, *args, engine=None, engine_kwargs=None, **kwargs):
nv.validate_window_func("mean", args, kwargs)
if maybe_use_numba(engine):
if self.method == "table":
raise NotImplementedError("method='table' is not supported.")
# Once numba supports np.nanmean with axis, args will be relevant.
# https://github.com/numba/numba/issues/6610
args = () if self.method == "single" else (0,)
func = generate_manual_numpy_nan_agg_with_axis(np.nanmean)
else:
func = np.nanmean

return self.apply(
np.nanmean,
func,
raw=True,
engine=engine,
engine_kwargs=engine_kwargs,
args=args,
)
window_func = window_aggregations.roll_mean
return self._apply(window_func, name="mean", **kwargs)
Expand Down Expand Up @@ -1584,16 +1581,15 @@ def mean(self, *args, engine=None, engine_kwargs=None, **kwargs):
def median(self, engine=None, engine_kwargs=None, **kwargs):
if maybe_use_numba(engine):
if self.method == "table":
raise NotImplementedError("method='table' is not supported.")
# Once numba supports np.nanmedian with axis, args will be relevant.
# https://github.com/numba/numba/issues/6610
args = () if self.method == "single" else (0,)
func = generate_manual_numpy_nan_agg_with_axis(np.nanmedian)
else:
func = np.nanmedian

return self.apply(
np.nanmedian,
func,
raw=True,
engine=engine,
engine_kwargs=engine_kwargs,
args=args,
)
window_func = window_aggregations.roll_median_c
return self._apply(window_func, name="median", **kwargs)
Expand Down
7 changes: 1 addition & 6 deletions pandas/tests/window/test_numba.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def test_invalid_kwargs_nopython():


@td.skip_if_no("numba", "0.46.0")
@pytest.mark.slow
@pytest.mark.filterwarnings("ignore:\\nThe keyword argument")
# Filter warnings when parallel=True and the function can't be parallelized by Numba
class TestTableMethod:
Expand All @@ -177,9 +178,6 @@ def f(x):
f, engine="numba", raw=True
)

@pytest.mark.xfail(
raises=NotImplementedError, reason="method='table' is not supported."
)
def test_table_method_rolling_methods(
self, axis, nogil, parallel, nopython, arithmetic_numba_supported_operators
):
Expand Down Expand Up @@ -247,9 +245,6 @@ def f(x):
)
tm.assert_frame_equal(result, expected)

@pytest.mark.xfail(
raises=NotImplementedError, reason="method='table' is not supported."
)
def test_table_method_expanding_methods(
self, axis, nogil, parallel, nopython, arithmetic_numba_supported_operators
):
Expand Down