-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
TYPING: pandas/core/window.py #27391
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
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 Set | ||
from typing import List, Optional, Set, Union | ||
import warnings | ||
|
||
import numpy as np | ||
|
@@ -63,19 +63,19 @@ class _Window(PandasObject, SelectionMixin): | |
"axis", | ||
"on", | ||
"closed", | ||
] | ||
] # type: List[str] | ||
exclusions = set() # type: Set[str] | ||
|
||
def __init__( | ||
self, | ||
obj, | ||
window=None, | ||
min_periods=None, | ||
center=False, | ||
win_type=None, | ||
axis=0, | ||
on=None, | ||
closed=None, | ||
min_periods: Optional[int] = None, | ||
center: Optional[bool] = False, | ||
win_type: Optional[str] = None, | ||
axis: Union[str, int] = 0, | ||
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 put this in pandas._typing as cc @topper-123 as this might conflict with your other PR |
||
on: Optional[str] = None, | ||
closed: Optional[str] = None, | ||
**kwargs | ||
): | ||
|
||
|
@@ -97,15 +97,15 @@ def _constructor(self): | |
return Window | ||
|
||
@property | ||
def is_datetimelike(self): | ||
def is_datetimelike(self) -> Optional[bool]: | ||
return None | ||
|
||
@property | ||
def _on(self): | ||
return None | ||
|
||
@property | ||
def is_freq_type(self): | ||
def is_freq_type(self) -> bool: | ||
return self.win_type == "freq" | ||
|
||
def validate(self): | ||
|
@@ -121,30 +121,20 @@ def validate(self): | |
]: | ||
raise ValueError("closed must be 'right', 'left', 'both' or " "'neither'") | ||
|
||
def _convert_freq(self): | ||
WillAyd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Resample according to the how, return a new object. | ||
""" | ||
obj = self._selected_obj | ||
index = None | ||
return obj, index | ||
|
||
def _create_blocks(self): | ||
""" | ||
Split data into blocks & return conformed data. | ||
""" | ||
|
||
obj, index = self._convert_freq() | ||
if index is not None: | ||
index = self._on | ||
obj = self._selected_obj | ||
|
||
# filter out the on from the object | ||
if self.on is not None: | ||
if obj.ndim == 2: | ||
obj = obj.reindex(columns=obj.columns.difference([self.on]), copy=False) | ||
blocks = obj._to_dict_of_blocks(copy=False).values() | ||
|
||
return blocks, obj, index | ||
return blocks, obj | ||
|
||
def _gotitem(self, key, ndim, subset=None): | ||
""" | ||
|
@@ -186,10 +176,10 @@ def _get_window(self, other=None): | |
return self.window | ||
|
||
@property | ||
def _window_type(self): | ||
def _window_type(self) -> str: | ||
return self.__class__.__name__ | ||
|
||
def __repr__(self): | ||
def __repr__(self) -> str: | ||
""" | ||
Provide a nice str repr of our rolling object. | ||
""" | ||
|
@@ -207,23 +197,21 @@ def __iter__(self): | |
url = "https://github.com/pandas-dev/pandas/issues/11704" | ||
raise NotImplementedError("See issue #11704 {url}".format(url=url)) | ||
|
||
def _get_index(self, index=None): | ||
def _get_index(self) -> Optional[np.ndarray]: | ||
""" | ||
Return index as ndarrays. | ||
Return index as an ndarray. | ||
|
||
Returns | ||
------- | ||
tuple of (index, index_as_ndarray) | ||
None or ndarray | ||
""" | ||
|
||
if self.is_freq_type: | ||
if index is None: | ||
index = self._on | ||
return index, index.asi8 | ||
return index, index | ||
|
||
def _prep_values(self, values=None, kill_inf=True): | ||
return self._on.asi8 | ||
return None | ||
|
||
def _prep_values(self, values: Optional[np.ndarray] = None) -> np.ndarray: | ||
"""Convert input to numpy arrays for Cython routines""" | ||
if values is None: | ||
values = getattr(self._selected_obj, "values", self._selected_obj) | ||
|
||
|
@@ -247,13 +235,14 @@ def _prep_values(self, values=None, kill_inf=True): | |
"cannot handle this type -> {0}" "".format(values.dtype) | ||
) | ||
|
||
if kill_inf: | ||
values = values.copy() | ||
values[np.isinf(values)] = np.NaN | ||
# Always convert inf to nan | ||
values[np.isinf(values)] = np.NaN | ||
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. You missed a copy call here. Results in kernc/backtesting.py#14 on write-protected arrays. |
||
|
||
return values | ||
|
||
def _wrap_result(self, result, block=None, obj=None): | ||
def _wrap_result( | ||
self, result, block=None, obj=None | ||
) -> Union[ABCSeries, ABCDataFrame]: | ||
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 use |
||
""" | ||
Wrap a single result. | ||
""" | ||
|
@@ -281,7 +270,9 @@ def _wrap_result(self, result, block=None, obj=None): | |
return type(obj)(result, index=index, columns=block.columns) | ||
return result | ||
|
||
def _wrap_results(self, results, blocks, obj, exclude=None): | ||
def _wrap_results( | ||
self, results, blocks, obj, exclude=None | ||
) -> Union[ABCSeries, ABCDataFrame]: | ||
""" | ||
Wrap the results. | ||
|
||
|
@@ -335,7 +326,7 @@ def _wrap_results(self, results, blocks, obj, exclude=None): | |
return obj.astype("float64") | ||
return concat(final, axis=1).reindex(columns=columns, copy=False) | ||
|
||
def _center_window(self, result, window): | ||
def _center_window(self, result, window) -> np.ndarray: | ||
""" | ||
Center the result in the window. | ||
""" | ||
|
@@ -724,7 +715,7 @@ def _apply_window(self, mean=True, **kwargs): | |
window = self._prep_window(**kwargs) | ||
center = self.center | ||
|
||
blocks, obj, index = self._create_blocks() | ||
blocks, obj = self._create_blocks() | ||
block_list = list(blocks) | ||
|
||
results = [] | ||
|
@@ -912,9 +903,9 @@ def _apply( | |
if check_minp is None: | ||
check_minp = _use_window | ||
|
||
blocks, obj, index = self._create_blocks() | ||
blocks, obj = self._create_blocks() | ||
block_list = list(blocks) | ||
index, indexi = self._get_index(index=index) | ||
index_as_array = self._get_index() | ||
|
||
results = [] | ||
exclude = [] | ||
|
@@ -947,7 +938,7 @@ def func(arg, window, min_periods=None, closed=None): | |
minp = check_minp(min_periods, window) | ||
# ensure we are only rolling on floats | ||
arg = ensure_float64(arg) | ||
return cfunc(arg, window, minp, indexi, closed, **kwargs) | ||
return cfunc(arg, window, minp, index_as_array, closed, **kwargs) | ||
|
||
# calculation function | ||
if center: | ||
|
@@ -1027,9 +1018,9 @@ class _Rolling_and_Expanding(_Rolling): | |
|
||
def count(self): | ||
|
||
blocks, obj, index = self._create_blocks() | ||
blocks, obj = self._create_blocks() | ||
# Validate the index | ||
self._get_index(index=index) | ||
self._get_index() | ||
|
||
window = self._get_window() | ||
window = min(window, len(obj)) if not self.center else window | ||
|
@@ -1088,11 +1079,10 @@ def count(self): | |
def apply(self, func, raw=None, args=(), kwargs={}): | ||
from pandas import Series | ||
|
||
# TODO: _level is unused? | ||
_level = kwargs.pop("_level", None) # noqa | ||
kwargs.pop("_level", None) | ||
window = self._get_window() | ||
offset = _offset(window, self.center) | ||
index, indexi = self._get_index() | ||
index_as_array = self._get_index() | ||
|
||
# TODO: default is for backward compat | ||
# change to False in the future | ||
|
@@ -1113,7 +1103,16 @@ def f(arg, window, min_periods, closed): | |
if not raw: | ||
arg = Series(arg, index=self.obj.index) | ||
return libwindow.roll_generic( | ||
arg, window, minp, indexi, closed, offset, func, raw, args, kwargs | ||
arg, | ||
window, | ||
minp, | ||
index_as_array, | ||
closed, | ||
offset, | ||
func, | ||
raw, | ||
args, | ||
kwargs, | ||
) | ||
|
||
return self._apply(f, func, args=args, kwargs=kwargs, center=False, raw=raw) | ||
|
@@ -1285,12 +1284,12 @@ def median(self, **kwargs): | |
def std(self, ddof=1, *args, **kwargs): | ||
nv.validate_window_func("std", args, kwargs) | ||
window = self._get_window() | ||
index, indexi = self._get_index() | ||
index_as_array = self._get_index() | ||
|
||
def f(arg, *args, **kwargs): | ||
minp = _require_min_periods(1)(self.min_periods, window) | ||
return _zsqrt( | ||
libwindow.roll_var(arg, window, minp, indexi, self.closed, ddof) | ||
libwindow.roll_var(arg, window, minp, index_as_array, self.closed, ddof) | ||
) | ||
|
||
return self._apply( | ||
|
@@ -1474,17 +1473,27 @@ def kurt(self, **kwargs): | |
|
||
def quantile(self, quantile, interpolation="linear", **kwargs): | ||
window = self._get_window() | ||
index, indexi = self._get_index() | ||
index_as_array = self._get_index() | ||
|
||
def f(arg, *args, **kwargs): | ||
minp = _use_window(self.min_periods, window) | ||
if quantile == 1.0: | ||
return libwindow.roll_max(arg, window, minp, indexi, self.closed) | ||
return libwindow.roll_max( | ||
arg, window, minp, index_as_array, self.closed | ||
) | ||
elif quantile == 0.0: | ||
return libwindow.roll_min(arg, window, minp, indexi, self.closed) | ||
return libwindow.roll_min( | ||
arg, window, minp, index_as_array, self.closed | ||
) | ||
else: | ||
return libwindow.roll_quantile( | ||
arg, window, minp, indexi, self.closed, quantile, interpolation | ||
arg, | ||
window, | ||
minp, | ||
index_as_array, | ||
self.closed, | ||
quantile, | ||
interpolation, | ||
) | ||
|
||
return self._apply(f, "quantile", quantile=quantile, **kwargs) | ||
|
@@ -2450,7 +2459,7 @@ def _apply(self, func, **kwargs): | |
------- | ||
y : same type as input argument | ||
""" | ||
blocks, obj, index = self._create_blocks() | ||
blocks, obj = self._create_blocks() | ||
block_list = list(blocks) | ||
|
||
results = [] | ||
|
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.
Was this actually throwing an error? Not opposed to it just surprised Mypy wouldn't infer this already
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.
Don't think so; just thought to go ahead and type.