Skip to content

Commit 6913e8d

Browse files
authored
CLN: Window code (#40292)
1 parent 5ef4159 commit 6913e8d

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

pandas/core/window/ewm.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ def __init__(
255255
self.ignore_na = ignore_na
256256
self.times = times
257257
if self.times is not None:
258-
if isinstance(times, str):
259-
self.times = self._selected_obj[times]
258+
if isinstance(self.times, str):
259+
self.times = self._selected_obj[self.times]
260260
if not is_datetime64_ns_dtype(self.times):
261261
raise ValueError("times must be datetime64[ns] dtype.")
262262
if len(self.times) != len(obj):
263263
raise ValueError("times must be the same length as the object.")
264-
if not isinstance(halflife, (str, datetime.timedelta)):
264+
if not isinstance(self.halflife, (str, datetime.timedelta)):
265265
raise ValueError(
266266
"halflife must be a string or datetime.timedelta object"
267267
)

pandas/core/window/rolling.py

+14-21
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
SelectionMixin,
6565
)
6666
import pandas.core.common as common
67-
from pandas.core.construction import extract_array
6867
from pandas.core.indexes.api import (
6968
Index,
7069
MultiIndex,
@@ -301,11 +300,8 @@ def __iter__(self):
301300
result = obj.iloc[slice(s, e)]
302301
yield result
303302

304-
def _prep_values(self, values: Optional[np.ndarray] = None) -> np.ndarray:
303+
def _prep_values(self, values: ArrayLike) -> np.ndarray:
305304
"""Convert input to numpy arrays for Cython routines"""
306-
if values is None:
307-
values = extract_array(self._selected_obj, extract_numpy=True)
308-
309305
if needs_i8_conversion(values.dtype):
310306
raise NotImplementedError(
311307
f"ops for {type(self).__name__} for this "
@@ -358,6 +354,16 @@ def _index_array(self):
358354
return self._on.asi8
359355
return None
360356

357+
def _resolve_output(self, out: DataFrame, obj: DataFrame) -> DataFrame:
358+
"""Validate and finalize result."""
359+
if out.shape[1] == 0 and obj.shape[1] > 0:
360+
raise DataError("No numeric types to aggregate")
361+
elif out.shape[1] == 0:
362+
return obj.astype("float64")
363+
364+
self._insert_on_column(out, obj)
365+
return out
366+
361367
def _get_window_indexer(self) -> BaseIndexer:
362368
"""
363369
Return an indexer class that will compute the window start and end bounds
@@ -421,13 +427,7 @@ def hfunc2d(values: ArrayLike) -> ArrayLike:
421427
new_mgr = mgr.apply(hfunc, ignore_failures=True)
422428
out = obj._constructor(new_mgr)
423429

424-
if out.shape[1] == 0 and obj.shape[1] > 0:
425-
raise DataError("No numeric types to aggregate")
426-
elif out.shape[1] == 0:
427-
return obj.astype("float64")
428-
429-
self._insert_on_column(out, obj)
430-
return out
430+
return self._resolve_output(out, obj)
431431

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

447-
if out.shape[1] == 0 and obj.shape[1] > 0:
448-
raise DataError("No numeric types to aggregate")
449-
elif out.shape[1] == 0:
450-
return obj.astype("float64")
451-
452-
self._insert_on_column(out, obj)
453-
return out
447+
return self._resolve_output(out, obj)
454448

455449
def _apply_pairwise(
456450
self,
@@ -2203,7 +2197,6 @@ def _get_window_indexer(self) -> GroupbyIndexer:
22032197
rolling_indexer: Type[BaseIndexer]
22042198
indexer_kwargs: Optional[Dict[str, Any]] = None
22052199
index_array = self._index_array
2206-
window = self.window
22072200
if isinstance(self.window, BaseIndexer):
22082201
rolling_indexer = type(self.window)
22092202
indexer_kwargs = self.window.__dict__
@@ -2216,7 +2209,7 @@ def _get_window_indexer(self) -> GroupbyIndexer:
22162209
window = self._win_freq_i8
22172210
else:
22182211
rolling_indexer = FixedWindowIndexer
2219-
index_array = None
2212+
window = self.window
22202213
window_indexer = GroupbyIndexer(
22212214
index_array=index_array,
22222215
window_size=window,

0 commit comments

Comments
 (0)