Skip to content

CLN: Window code #40292

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 3 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions pandas/core/window/ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,13 @@ def __init__(
self.ignore_na = ignore_na
self.times = times
if self.times is not None:
if isinstance(times, str):
self.times = self._selected_obj[times]
if isinstance(self.times, str):
self.times = self._selected_obj[self.times]
if not is_datetime64_ns_dtype(self.times):
raise ValueError("times must be datetime64[ns] dtype.")
if len(self.times) != len(obj):
raise ValueError("times must be the same length as the object.")
if not isinstance(halflife, (str, datetime.timedelta)):
if not isinstance(self.halflife, (str, datetime.timedelta)):
raise ValueError(
"halflife must be a string or datetime.timedelta object"
)
Expand Down
35 changes: 14 additions & 21 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
SelectionMixin,
)
import pandas.core.common as common
from pandas.core.construction import extract_array
from pandas.core.indexes.api import (
Index,
MultiIndex,
Expand Down Expand Up @@ -301,11 +300,8 @@ def __iter__(self):
result = obj.iloc[slice(s, e)]
yield result

def _prep_values(self, values: Optional[np.ndarray] = None) -> np.ndarray:
def _prep_values(self, values: ArrayLike) -> np.ndarray:
"""Convert input to numpy arrays for Cython routines"""
if values is None:
values = extract_array(self._selected_obj, extract_numpy=True)

if needs_i8_conversion(values.dtype):
raise NotImplementedError(
f"ops for {type(self).__name__} for this "
Expand Down Expand Up @@ -358,6 +354,16 @@ def _index_array(self):
return self._on.asi8
return None

def _resolve_output(self, out: DataFrame, obj: DataFrame) -> DataFrame:
"""Validate and finalize result."""
if out.shape[1] == 0 and obj.shape[1] > 0:
raise DataError("No numeric types to aggregate")
elif out.shape[1] == 0:
return obj.astype("float64")

self._insert_on_column(out, obj)
return out

def _get_window_indexer(self) -> BaseIndexer:
"""
Return an indexer class that will compute the window start and end bounds
Expand Down Expand Up @@ -421,13 +427,7 @@ def hfunc2d(values: ArrayLike) -> ArrayLike:
new_mgr = mgr.apply(hfunc, ignore_failures=True)
out = obj._constructor(new_mgr)

if out.shape[1] == 0 and obj.shape[1] > 0:
raise DataError("No numeric types to aggregate")
elif out.shape[1] == 0:
return obj.astype("float64")

self._insert_on_column(out, obj)
return out
return self._resolve_output(out, obj)

def _apply_tablewise(
self, homogeneous_func: Callable[..., ArrayLike], name: Optional[str] = None
Expand All @@ -444,13 +444,7 @@ def _apply_tablewise(
result = result.T if self.axis == 1 else result
out = obj._constructor(result, index=obj.index, columns=obj.columns)

if out.shape[1] == 0 and obj.shape[1] > 0:
raise DataError("No numeric types to aggregate")
elif out.shape[1] == 0:
return obj.astype("float64")

self._insert_on_column(out, obj)
return out
return self._resolve_output(out, obj)

def _apply_pairwise(
self,
Expand Down Expand Up @@ -2203,7 +2197,6 @@ def _get_window_indexer(self) -> GroupbyIndexer:
rolling_indexer: Type[BaseIndexer]
indexer_kwargs: Optional[Dict[str, Any]] = None
index_array = self._index_array
window = self.window
if isinstance(self.window, BaseIndexer):
rolling_indexer = type(self.window)
indexer_kwargs = self.window.__dict__
Expand All @@ -2216,7 +2209,7 @@ def _get_window_indexer(self) -> GroupbyIndexer:
window = self._win_freq_i8
else:
rolling_indexer = FixedWindowIndexer
index_array = None
window = self.window
window_indexer = GroupbyIndexer(
index_array=index_array,
window_size=window,
Expand Down