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
Changes from 1 commit
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
54 changes: 44 additions & 10 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo


Parameters
----------
other:
ignored, exists for compatibility

Returns
-------
window : int
"""
return self.window

@property
Expand Down Expand Up @@ -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
----------
Expand All @@ -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):
Expand All @@ -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.

Expand All @@ -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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 rolling function

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add what kwargs is here

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
-------
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
minp = check_minp(min_periods, len(window))
return cfunc(arg, window, minp)
Expand Down