diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py index 5a71db82f26e4..6310a751492da 100644 --- a/pandas/core/window/ewm.py +++ b/pandas/core/window/ewm.py @@ -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" ) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 299e1755c0025..503849bf673d5 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -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, @@ -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 " @@ -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 @@ -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 @@ -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, @@ -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__ @@ -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,