From bda3c7c2c1a740ee0aa2aa73479fcef83db79af2 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 15 Aug 2020 12:15:26 -0700 Subject: [PATCH 1/3] REF: _apply_blockwise define exclude in terms of skipped --- pandas/core/window/rolling.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 966773b7c6982..8d482b504914c 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -12,7 +12,7 @@ from pandas._libs.tslibs import BaseOffset, to_offset import pandas._libs.window.aggregations as window_aggregations -from pandas._typing import ArrayLike, Axis, FrameOrSeries, Scalar +from pandas._typing import ArrayLike, Axis, FrameOrSeries, Label from pandas.compat._optional import import_optional_dependency from pandas.compat.numpy import function as nv from pandas.util._decorators import Appender, Substitution, cache_readonly, doc @@ -381,21 +381,33 @@ 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) -> FrameOrSeries: + def _wrap_results(self, results, obj, skipped: List[int]) -> FrameOrSeries: """ Wrap the results. Parameters ---------- results : list of ndarrays - blocks : list of blocks obj : conformed data (may be resampled) - exclude: list of columns to exclude, default to None + skipped: List[int] + Indices of blocks that are skipped. """ from pandas import Series, concat + exclude: List[Label] + if obj.ndim == 2: + orig_blocks = list(obj._to_dict_of_blocks(copy=False).values()) + exclude = [] + for i in skipped: + exclude.extend(orig_blocks[i].columns) + else: + orig_blocks = [obj] + exclude = [] + + kept_blocks = [blk for i, blk in enumerate(orig_blocks) if i not in skipped] + final = [] - for result, block in zip(results, blocks): + for result, block in zip(results, kept_blocks): result = self._wrap_result(result, block=block, obj=obj) if result.ndim == 1: @@ -500,7 +512,6 @@ def _apply_blockwise( skipped: List[int] = [] results: List[ArrayLike] = [] - exclude: List[Scalar] = [] for i, b in enumerate(blocks): try: values = self._prep_values(b.values) @@ -508,7 +519,6 @@ def _apply_blockwise( except (TypeError, NotImplementedError) as err: if isinstance(obj, ABCDataFrame): skipped.append(i) - exclude.extend(b.columns) continue else: raise DataError("No numeric types to aggregate") from err @@ -516,8 +526,7 @@ def _apply_blockwise( result = homogeneous_func(values) results.append(result) - block_list = [blk for i, blk in enumerate(blocks) if i not in skipped] - return self._wrap_results(results, block_list, obj, exclude) + return self._wrap_results(results, obj, skipped) def _apply( self, @@ -1292,7 +1301,7 @@ def count(self): ).sum() results.append(result) - return self._wrap_results(results, blocks, obj) + return self._wrap_results(results, obj, skipped=[]) _shared_docs["apply"] = dedent( r""" @@ -2277,6 +2286,7 @@ def _get_window_indexer(self, window: int) -> GroupbyRollingIndexer: if isinstance(self.window, BaseIndexer): rolling_indexer = type(self.window) indexer_kwargs = self.window.__dict__ + assert isinstance(indexer_kwargs, dict) # We'll be using the index of each group later indexer_kwargs.pop("index_array", None) elif self.is_freq_type: From 75753d83134fa2bbad529422ee2a35c4ffaabeaa Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 19 Aug 2020 16:12:19 -0700 Subject: [PATCH 2/3] define exclude only once --- pandas/core/window/rolling.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 8d482b504914c..dbf0c15ca1b19 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -394,15 +394,13 @@ def _wrap_results(self, results, obj, skipped: List[int]) -> FrameOrSeries: """ from pandas import Series, concat - exclude: List[Label] + exclude: List[Label] = [] if obj.ndim == 2: orig_blocks = list(obj._to_dict_of_blocks(copy=False).values()) - exclude = [] for i in skipped: exclude.extend(orig_blocks[i].columns) else: orig_blocks = [obj] - exclude = [] kept_blocks = [blk for i, blk in enumerate(orig_blocks) if i not in skipped] From 30b1e8df00c4add3f1add62a51ea770a3440c239 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 19 Aug 2020 19:12:22 -0700 Subject: [PATCH 3/3] dummy to force Travis