Skip to content

Commit 020040b

Browse files
authored
REF/TYP: pandas/core/window/*.py (#37091)
1 parent a838759 commit 020040b

File tree

4 files changed

+187
-190
lines changed

4 files changed

+187
-190
lines changed

pandas/_libs/window/aggregations.pyx

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import cython
44
from cython import Py_ssize_t
55

6-
from libc.stdlib cimport free, malloc
76
from libcpp.deque cimport deque
87

98
import numpy as np
@@ -906,7 +905,7 @@ interpolation_types = {
906905

907906

908907
def roll_quantile(ndarray[float64_t, cast=True] values, ndarray[int64_t] start,
909-
ndarray[int64_t] end, int64_t minp, int64_t win,
908+
ndarray[int64_t] end, int64_t minp,
910909
float64_t quantile, str interpolation):
911910
"""
912911
O(N log(window)) implementation using skip list
@@ -933,7 +932,7 @@ def roll_quantile(ndarray[float64_t, cast=True] values, ndarray[int64_t] start,
933932
# actual skiplist ops outweigh any window computation costs
934933
output = np.empty(N, dtype=float)
935934

936-
if win == 0 or (end - start).max() == 0:
935+
if (end - start).max() == 0:
937936
output[:] = NaN
938937
return output
939938
win = (end - start).max()
@@ -1020,7 +1019,7 @@ def roll_quantile(ndarray[float64_t, cast=True] values, ndarray[int64_t] start,
10201019
def roll_apply(object obj,
10211020
ndarray[int64_t] start, ndarray[int64_t] end,
10221021
int64_t minp,
1023-
object func, bint raw,
1022+
object function, bint raw,
10241023
tuple args, dict kwargs):
10251024
cdef:
10261025
ndarray[float64_t] output, counts
@@ -1048,9 +1047,9 @@ def roll_apply(object obj,
10481047

10491048
if counts[i] >= minp:
10501049
if raw:
1051-
output[i] = func(arr[s:e], *args, **kwargs)
1050+
output[i] = function(arr[s:e], *args, **kwargs)
10521051
else:
1053-
output[i] = func(obj.iloc[s:e], *args, **kwargs)
1052+
output[i] = function(obj.iloc[s:e], *args, **kwargs)
10541053
else:
10551054
output[i] = NaN
10561055

pandas/core/window/ewm.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime
22
from functools import partial
33
from textwrap import dedent
4-
from typing import Optional, Union
4+
from typing import TYPE_CHECKING, Optional, Union
55

66
import numpy as np
77

@@ -17,6 +17,10 @@
1717
from pandas.core.window.common import _doc_template, _shared_docs, zsqrt
1818
from pandas.core.window.rolling import BaseWindow, flex_binary_moment
1919

20+
if TYPE_CHECKING:
21+
from pandas import Series
22+
23+
2024
_bias_template = """
2125
Parameters
2226
----------
@@ -60,6 +64,15 @@ def get_center_of_mass(
6064
return float(comass)
6165

6266

67+
def wrap_result(obj: "Series", result: np.ndarray) -> "Series":
68+
"""
69+
Wrap a single 1D result.
70+
"""
71+
obj = obj._selected_obj
72+
73+
return obj._constructor(result, obj.index, name=obj.name)
74+
75+
6376
class ExponentialMovingWindow(BaseWindow):
6477
r"""
6578
Provide exponential weighted (EW) functions.
@@ -413,7 +426,7 @@ def _get_cov(X, Y):
413426
self.min_periods,
414427
bias,
415428
)
416-
return X._wrap_result(cov)
429+
return wrap_result(X, cov)
417430

418431
return flex_binary_moment(
419432
self._selected_obj, other._selected_obj, _get_cov, pairwise=bool(pairwise)
@@ -467,7 +480,7 @@ def _cov(x, y):
467480
x_var = _cov(x_values, x_values)
468481
y_var = _cov(y_values, y_values)
469482
corr = cov / zsqrt(x_var * y_var)
470-
return X._wrap_result(corr)
483+
return wrap_result(X, corr)
471484

472485
return flex_binary_moment(
473486
self._selected_obj, other._selected_obj, _get_corr, pairwise=bool(pairwise)

pandas/core/window/expanding.py

+26-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from textwrap import dedent
2-
from typing import Dict, Optional
2+
from typing import Any, Callable, Dict, Optional, Tuple, Union
33

4+
import numpy as np
5+
6+
from pandas._typing import FrameOrSeries
47
from pandas.compat.numpy import function as nv
58
from pandas.util._decorators import Appender, Substitution, doc
69

@@ -65,7 +68,9 @@ def __init__(self, obj, min_periods=1, center=None, axis=0, **kwargs):
6568
def _constructor(self):
6669
return Expanding
6770

68-
def _get_window(self, other=None, **kwargs):
71+
def _get_window(
72+
self, other: Optional[Union[np.ndarray, FrameOrSeries]] = None, **kwargs
73+
) -> int:
6974
"""
7075
Get the window length over which to perform some operation.
7176
@@ -135,12 +140,12 @@ def count(self, **kwargs):
135140
@Appender(_shared_docs["apply"])
136141
def apply(
137142
self,
138-
func,
143+
func: Callable[..., Any],
139144
raw: bool = False,
140145
engine: Optional[str] = None,
141146
engine_kwargs: Optional[Dict[str, bool]] = None,
142-
args=None,
143-
kwargs=None,
147+
args: Optional[Tuple[Any, ...]] = None,
148+
kwargs: Optional[Dict[str, Any]] = None,
144149
):
145150
return super().apply(
146151
func,
@@ -183,19 +188,19 @@ def median(self, **kwargs):
183188

184189
@Substitution(name="expanding", versionadded="")
185190
@Appender(_shared_docs["std"])
186-
def std(self, ddof=1, *args, **kwargs):
191+
def std(self, ddof: int = 1, *args, **kwargs):
187192
nv.validate_expanding_func("std", args, kwargs)
188193
return super().std(ddof=ddof, **kwargs)
189194

190195
@Substitution(name="expanding", versionadded="")
191196
@Appender(_shared_docs["var"])
192-
def var(self, ddof=1, *args, **kwargs):
197+
def var(self, ddof: int = 1, *args, **kwargs):
193198
nv.validate_expanding_func("var", args, kwargs)
194199
return super().var(ddof=ddof, **kwargs)
195200

196201
@Substitution(name="expanding")
197202
@Appender(_shared_docs["sem"])
198-
def sem(self, ddof=1, *args, **kwargs):
203+
def sem(self, ddof: int = 1, *args, **kwargs):
199204
return super().sem(ddof=ddof, **kwargs)
200205

201206
@Substitution(name="expanding", func_name="skew")
@@ -245,12 +250,23 @@ def quantile(self, quantile, interpolation="linear", **kwargs):
245250
@Substitution(name="expanding", func_name="cov")
246251
@Appender(_doc_template)
247252
@Appender(_shared_docs["cov"])
248-
def cov(self, other=None, pairwise=None, ddof=1, **kwargs):
253+
def cov(
254+
self,
255+
other: Optional[Union[np.ndarray, FrameOrSeries]] = None,
256+
pairwise: Optional[bool] = None,
257+
ddof: int = 1,
258+
**kwargs,
259+
):
249260
return super().cov(other=other, pairwise=pairwise, ddof=ddof, **kwargs)
250261

251262
@Substitution(name="expanding")
252263
@Appender(_shared_docs["corr"])
253-
def corr(self, other=None, pairwise=None, **kwargs):
264+
def corr(
265+
self,
266+
other: Optional[Union[np.ndarray, FrameOrSeries]] = None,
267+
pairwise: Optional[bool] = None,
268+
**kwargs,
269+
):
254270
return super().corr(other=other, pairwise=pairwise, **kwargs)
255271

256272

0 commit comments

Comments
 (0)