Skip to content

Commit 9468071

Browse files
authored
REF: _apply_blockwise define exclude in terms of skipped (#35740)
1 parent 7e91937 commit 9468071

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

pandas/core/window/rolling.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from pandas._libs.tslibs import BaseOffset, to_offset
1414
import pandas._libs.window.aggregations as window_aggregations
15-
from pandas._typing import ArrayLike, Axis, FrameOrSeries, Scalar
15+
from pandas._typing import ArrayLike, Axis, FrameOrSeries, Label
1616
from pandas.compat._optional import import_optional_dependency
1717
from pandas.compat.numpy import function as nv
1818
from pandas.util._decorators import Appender, Substitution, cache_readonly, doc
@@ -381,21 +381,31 @@ def _wrap_result(self, result, block=None, obj=None):
381381
return type(obj)(result, index=index, columns=block.columns)
382382
return result
383383

384-
def _wrap_results(self, results, blocks, obj, exclude=None) -> FrameOrSeries:
384+
def _wrap_results(self, results, obj, skipped: List[int]) -> FrameOrSeries:
385385
"""
386386
Wrap the results.
387387
388388
Parameters
389389
----------
390390
results : list of ndarrays
391-
blocks : list of blocks
392391
obj : conformed data (may be resampled)
393-
exclude: list of columns to exclude, default to None
392+
skipped: List[int]
393+
Indices of blocks that are skipped.
394394
"""
395395
from pandas import Series, concat
396396

397+
exclude: List[Label] = []
398+
if obj.ndim == 2:
399+
orig_blocks = list(obj._to_dict_of_blocks(copy=False).values())
400+
for i in skipped:
401+
exclude.extend(orig_blocks[i].columns)
402+
else:
403+
orig_blocks = [obj]
404+
405+
kept_blocks = [blk for i, blk in enumerate(orig_blocks) if i not in skipped]
406+
397407
final = []
398-
for result, block in zip(results, blocks):
408+
for result, block in zip(results, kept_blocks):
399409

400410
result = self._wrap_result(result, block=block, obj=obj)
401411
if result.ndim == 1:
@@ -491,24 +501,21 @@ def _apply_blockwise(
491501

492502
skipped: List[int] = []
493503
results: List[ArrayLike] = []
494-
exclude: List[Scalar] = []
495504
for i, b in enumerate(blocks):
496505
try:
497506
values = self._prep_values(b.values)
498507

499508
except (TypeError, NotImplementedError) as err:
500509
if isinstance(obj, ABCDataFrame):
501510
skipped.append(i)
502-
exclude.extend(b.columns)
503511
continue
504512
else:
505513
raise DataError("No numeric types to aggregate") from err
506514

507515
result = homogeneous_func(values)
508516
results.append(result)
509517

510-
block_list = [blk for i, blk in enumerate(blocks) if i not in skipped]
511-
return self._wrap_results(results, block_list, obj, exclude)
518+
return self._wrap_results(results, obj, skipped)
512519

513520
def _apply(
514521
self,
@@ -1283,7 +1290,7 @@ def count(self):
12831290
).sum()
12841291
results.append(result)
12851292

1286-
return self._wrap_results(results, blocks, obj)
1293+
return self._wrap_results(results, obj, skipped=[])
12871294

12881295
_shared_docs["apply"] = dedent(
12891296
r"""

0 commit comments

Comments
 (0)