Skip to content

CLN: Unify Window._apply_window and Rolling._apply functions #27403

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 15 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 21 additions & 3 deletions pandas/_libs/window.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1675,9 +1675,25 @@ def roll_generic(object obj,
return output


def roll_window(ndarray[float64_t, ndim=1, cast=True] values,
ndarray[float64_t, ndim=1, cast=True] weights,
int minp, bint avg=True):
# ----------------------------------------------------------------------
# Rolling sum and mean for weighted window


def roll_window_sum(ndarray[float64_t, ndim=1, cast=True] values,
ndarray[float64_t, ndim=1, cast=True] weights,
int minp):
return _roll_window_sum_mean(values, weights, minp, avg=0)


def roll_window_mean(ndarray[float64_t, ndim=1, cast=True] values,
ndarray[float64_t, ndim=1, cast=True] weights,
int minp):
return _roll_window_sum_mean(values, weights, minp, avg=1)


def _roll_window_sum_mean(ndarray[float64_t, ndim=1, cast=True] values,
ndarray[float64_t, ndim=1, cast=True] weights,
int minp, bint avg=True):
"""
Assume len(weights) << len(values)
"""
Expand All @@ -1688,6 +1704,7 @@ def roll_window(ndarray[float64_t, ndim=1, cast=True] values,

in_n = len(values)
win_n = len(weights)

output = np.zeros(in_n, dtype=float)
counts = np.zeros(in_n, dtype=float)
if avg:
Expand Down Expand Up @@ -1739,6 +1756,7 @@ def roll_window(ndarray[float64_t, ndim=1, cast=True] values,

return output


# ----------------------------------------------------------------------
# Exponentially weighted moving average

Expand Down
15 changes: 8 additions & 7 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,14 +707,14 @@ def _pop_args(win_type, arg_names, kwargs):
# GH #15662. `False` makes symmetric window, rather than periodic.
return sig.get_window(win_type, window, False).astype(float)

def _apply_window(self, mean=True, **kwargs):
def _apply_window(self, func, **kwargs):
"""
Applies a moving window of type ``window_type`` on the data.

Parameters
----------
mean : bool, default True
If True computes weighted mean, else weighted sum
func : str
Name of function to apply

Returns
-------
Expand Down Expand Up @@ -749,12 +749,13 @@ def _apply_window(self, mean=True, **kwargs):
additional_nans = np.array([np.NaN] * offset)

def f(arg, *args, **kwargs):
cfunc = getattr(libwindow, func)
minp = _use_window(self.min_periods, len(window))
return libwindow.roll_window(

return cfunc(
np.concatenate((arg, additional_nans)) if center else arg,
window,
minp,
avg=mean,
)

result = np.apply_along_axis(f, self.axis, values)
Expand Down Expand Up @@ -831,13 +832,13 @@ def aggregate(self, arg, *args, **kwargs):
@Appender(_shared_docs["sum"])
def sum(self, *args, **kwargs):
nv.validate_window_func("sum", args, kwargs)
return self._apply_window(mean=False, **kwargs)
return self._apply_window("roll_window_sum", **kwargs)

@Substitution(name="window")
@Appender(_shared_docs["mean"])
def mean(self, *args, **kwargs):
nv.validate_window_func("mean", args, kwargs)
return self._apply_window(mean=True, **kwargs)
return self._apply_window("roll_window_mean", **kwargs)


class _GroupByMixin(GroupByMixin):
Expand Down