-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
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
Changes from 1 commit
fb2ef95
646bb95
d387958
753f876
46bec7b
b4e6f69
ea76fc9
8823ee9
7b09f24
e2d2501
6494a1e
68d2b16
cc37244
96787e7
69994d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
from collections import defaultdict | ||
from datetime import timedelta | ||
from textwrap import dedent | ||
from typing import Callable, List, Optional, Set | ||
from typing import Callable, List, Optional, Set, Union | ||
import warnings | ||
|
||
import numpy as np | ||
|
@@ -172,7 +172,19 @@ def __getattr__(self, attr): | |
def _dir_additions(self): | ||
return self.obj._dir_additions() | ||
|
||
def _get_window(self, other=None, **kwargs): | ||
def _get_window(self, other=None, **kwargs) -> int: | ||
""" | ||
Returns window lenght | ||
|
||
Parameters | ||
---------- | ||
other: | ||
ignored, exists for compatibility | ||
|
||
Returns | ||
------- | ||
window : int | ||
""" | ||
return self.window | ||
ihsansecer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@property | ||
|
@@ -341,9 +353,11 @@ def _center_window(self, result, window) -> np.ndarray: | |
result = np.copy(result[tuple(lead_indexer)]) | ||
return result | ||
|
||
def _wrap_roll(self, cfunc, check_minp, index, **kwargs) -> Callable: | ||
def _get_roll_func( | ||
self, cfunc: Callable, check_minp: Callable, index: np.ndarray, **kwargs | ||
) -> Callable: | ||
""" | ||
Wrap rolling function. | ||
Wrap rolling function to check values passed. | ||
|
||
Parameters | ||
---------- | ||
|
@@ -353,6 +367,10 @@ def _wrap_roll(self, cfunc, check_minp, index, **kwargs) -> Callable: | |
function to check minimum period parameter | ||
index : ndarray | ||
used for variable window | ||
|
||
Returns | ||
------- | ||
func : callable | ||
""" | ||
|
||
def func(arg, window, min_periods=None, closed=None): | ||
|
@@ -362,8 +380,14 @@ def func(arg, window, min_periods=None, closed=None): | |
return func | ||
|
||
def _apply( | ||
self, func, name=None, window=None, center=None, check_minp=None, **kwargs | ||
): | ||
self, | ||
func: Union[str, Callable], | ||
name: Optional[str] = None, | ||
window: Optional[Union[int, str]] = None, | ||
center: Optional[bool] = None, | ||
check_minp: Optional[Callable] = None, | ||
**kwargs | ||
) -> FrameOrSeries: | ||
""" | ||
Rolling statistical measure using supplied function. | ||
|
||
|
@@ -374,9 +398,12 @@ def _apply( | |
func : str/callable to apply | ||
name : str, optional | ||
name of this function | ||
window : int/array, default to _get_window() | ||
window : int/str, default to _get_window() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. didn't see any passing array to _apply. This seems to be an offset or int same as in |
||
window lenght or offset | ||
center : bool, default to self.center | ||
check_minp : function, default to _use_window | ||
**kwargs | ||
additional arguments for rolling function and window function | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add what kwargs is here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we modify kwargs (or copy of it) to remove window function (passed to _get_window) kwargs first. window rolling functions (passed to _get_roll_func) don't take kwargs but when they do it will be a problem |
||
Returns | ||
------- | ||
|
@@ -422,7 +449,7 @@ def _apply( | |
"in libwindow.{func}".format(func=func) | ||
) | ||
|
||
func = self._wrap_roll(cfunc, check_minp, index_as_array, **kwargs) | ||
func = self._get_roll_func(cfunc, check_minp, index_as_array, **kwargs) | ||
|
||
# calculation function | ||
if center: | ||
|
@@ -765,11 +792,16 @@ def validate(self): | |
else: | ||
raise ValueError("Invalid window {0}".format(window)) | ||
|
||
def _get_window(self, **kwargs): | ||
def _get_window(self, other=None, **kwargs) -> np.ndarray: | ||
""" | ||
Provide validation for the window type, return the window | ||
which has already been validated. | ||
|
||
Parameters | ||
---------- | ||
other: | ||
ignored, exists for compatibility | ||
|
||
Returns | ||
------- | ||
window : ndarray | ||
|
@@ -816,7 +848,9 @@ 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 _wrap_roll(self, cfunc, check_minp, *args, **kwargs): | ||
def _get_roll_func( | ||
self, cfunc: Callable, check_minp: Callable, index: np.ndarray, **kwargs | ||
) -> Callable: | ||
def func(arg, window, min_periods=None, closed=None): | ||
ihsansecer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
minp = check_minp(min_periods, len(window)) | ||
return cfunc(arg, window, minp) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo