Skip to content

Commit 7e05bcd

Browse files
authored
PERF: DataFrame._reduce (#43243)
1 parent dac3a8a commit 7e05bcd

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

pandas/core/frame.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9826,26 +9826,28 @@ def _reduce(
98269826
assert filter_type is None or filter_type == "bool", filter_type
98279827
out_dtype = "bool" if filter_type == "bool" else None
98289828

9829-
own_dtypes = [arr.dtype for arr in self._iter_column_arrays()]
9829+
if numeric_only is None and name in ["mean", "median"]:
9830+
own_dtypes = [arr.dtype for arr in self._mgr.arrays]
98309831

9831-
dtype_is_dt = np.array(
9832-
[is_datetime64_any_dtype(dtype) for dtype in own_dtypes],
9833-
dtype=bool,
9834-
)
9835-
if numeric_only is None and name in ["mean", "median"] and dtype_is_dt.any():
9836-
warnings.warn(
9837-
"DataFrame.mean and DataFrame.median with numeric_only=None "
9838-
"will include datetime64 and datetime64tz columns in a "
9839-
"future version.",
9840-
FutureWarning,
9841-
stacklevel=5,
9832+
dtype_is_dt = np.array(
9833+
[is_datetime64_any_dtype(dtype) for dtype in own_dtypes],
9834+
dtype=bool,
98429835
)
9843-
# Non-copy equivalent to
9844-
# cols = self.columns[~dtype_is_dt]
9845-
# self = self[cols]
9846-
predicate = lambda x: not is_datetime64_any_dtype(x.dtype)
9847-
mgr = self._mgr._get_data_subset(predicate)
9848-
self = type(self)(mgr)
9836+
if dtype_is_dt.any():
9837+
warnings.warn(
9838+
"DataFrame.mean and DataFrame.median with numeric_only=None "
9839+
"will include datetime64 and datetime64tz columns in a "
9840+
"future version.",
9841+
FutureWarning,
9842+
stacklevel=5,
9843+
)
9844+
# Non-copy equivalent to
9845+
# dt64_cols = self.dtypes.apply(is_datetime64_any_dtype)
9846+
# cols = self.columns[~dt64_cols]
9847+
# self = self[cols]
9848+
predicate = lambda x: not is_datetime64_any_dtype(x.dtype)
9849+
mgr = self._mgr._get_data_subset(predicate)
9850+
self = type(self)(mgr)
98499851

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

0 commit comments

Comments
 (0)