Skip to content

REF: _apply_blockwise define exclude in terms of skipped #35740

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 7 commits into from
Aug 21, 2020
30 changes: 20 additions & 10 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can assign = [] here (and remove from below

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:
Expand Down Expand Up @@ -500,24 +512,21 @@ 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)

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

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,
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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:
Expand Down