|
1 | 1 | """Common utility functions for rolling operations""" |
2 | 2 | from collections import defaultdict |
3 | | -from typing import Callable, Optional |
4 | 3 | import warnings |
5 | 4 |
|
6 | 5 | import numpy as np |
7 | 6 |
|
8 | 7 | from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries |
9 | 8 |
|
10 | | -from pandas.core.groupby.base import GotItemMixin |
11 | 9 | from pandas.core.indexes.api import MultiIndex |
12 | 10 | from pandas.core.shared_docs import _shared_docs |
13 | 11 |
|
|
27 | 25 | """ |
28 | 26 |
|
29 | 27 |
|
30 | | -def _dispatch(name: str, *args, **kwargs): |
31 | | - """ |
32 | | - Dispatch to apply. |
33 | | - """ |
34 | | - |
35 | | - def outer(self, *args, **kwargs): |
36 | | - def f(x): |
37 | | - x = self._shallow_copy(x, groupby=self._groupby) |
38 | | - return getattr(x, name)(*args, **kwargs) |
39 | | - |
40 | | - return self._groupby.apply(f) |
41 | | - |
42 | | - outer.__name__ = name |
43 | | - return outer |
44 | | - |
45 | | - |
46 | | -class WindowGroupByMixin(GotItemMixin): |
47 | | - """ |
48 | | - Provide the groupby facilities. |
49 | | - """ |
50 | | - |
51 | | - def __init__(self, obj, *args, **kwargs): |
52 | | - kwargs.pop("parent", None) |
53 | | - groupby = kwargs.pop("groupby", None) |
54 | | - if groupby is None: |
55 | | - groupby, obj = obj, obj._selected_obj |
56 | | - self._groupby = groupby |
57 | | - self._groupby.mutated = True |
58 | | - self._groupby.grouper.mutated = True |
59 | | - super().__init__(obj, *args, **kwargs) |
60 | | - |
61 | | - corr = _dispatch("corr", other=None, pairwise=None) |
62 | | - cov = _dispatch("cov", other=None, pairwise=None) |
63 | | - |
64 | | - def _apply( |
65 | | - self, |
66 | | - func: Callable, |
67 | | - require_min_periods: int = 0, |
68 | | - floor: int = 1, |
69 | | - is_weighted: bool = False, |
70 | | - name: Optional[str] = None, |
71 | | - use_numba_cache: bool = False, |
72 | | - **kwargs, |
73 | | - ): |
74 | | - """ |
75 | | - Dispatch to apply; we are stripping all of the _apply kwargs and |
76 | | - performing the original function call on the grouped object. |
77 | | - """ |
78 | | - kwargs.pop("floor", None) |
79 | | - kwargs.pop("original_func", None) |
80 | | - |
81 | | - # TODO: can we de-duplicate with _dispatch? |
82 | | - def f(x, name=name, *args): |
83 | | - x = self._shallow_copy(x) |
84 | | - |
85 | | - if isinstance(name, str): |
86 | | - return getattr(x, name)(*args, **kwargs) |
87 | | - |
88 | | - return x.apply(name, *args, **kwargs) |
89 | | - |
90 | | - return self._groupby.apply(f) |
91 | | - |
92 | | - |
93 | 28 | def flex_binary_moment(arg1, arg2, f, pairwise=False): |
94 | 29 |
|
95 | 30 | if not ( |
|
0 commit comments