Skip to content

Commit fa0052e

Browse files
authored
PERF: Avoid copy in DataFrame._reduce corner cases (#42232)
1 parent f463224 commit fa0052e

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

pandas/core/frame.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -9810,8 +9810,12 @@ def _reduce(
98109810
FutureWarning,
98119811
stacklevel=5,
98129812
)
9813-
cols = self.columns[~dtype_is_dt]
9814-
self = self[cols]
9813+
# Non-copy equivalent to
9814+
# cols = self.columns[~dtype_is_dt]
9815+
# self = self[cols]
9816+
predicate = lambda x: not is_datetime64_any_dtype(x.dtype)
9817+
mgr = self._mgr._get_data_subset(predicate)
9818+
self = type(self)(mgr)
98159819

98169820
# TODO: Make other agg func handle axis=None properly GH#21597
98179821
axis = self._get_axis_number(axis)

pandas/core/internals/managers.py

+4
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ def is_view(self) -> bool:
479479

480480
return False
481481

482+
def _get_data_subset(self: T, predicate: Callable) -> T:
483+
blocks = [blk for blk in self.blocks if predicate(blk.values)]
484+
return self._combine(blocks, copy=False)
485+
482486
def get_bool_data(self: T, copy: bool = False) -> T:
483487
"""
484488
Select blocks that are bool-dtype and columns from object-dtype blocks

0 commit comments

Comments
 (0)