Skip to content

Commit 60cc1fa

Browse files
jbrockmendelSeeminSyed
authored andcommitted
REF: put all post-processing at end of DataFrame._reduce (pandas-dev#32671)
1 parent 67bcbe1 commit 60cc1fa

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

pandas/core/frame.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -7808,6 +7808,8 @@ def _reduce(
78087808
self, op, name, axis=0, skipna=True, numeric_only=None, filter_type=None, **kwds
78097809
):
78107810

7811+
assert filter_type is None or filter_type == "bool", filter_type
7812+
78117813
dtype_is_dt = self.dtypes.apply(
78127814
lambda x: is_datetime64_any_dtype(x) or is_period_dtype(x)
78137815
)
@@ -7835,7 +7837,7 @@ def f(x):
78357837
return op(x, axis=axis, skipna=skipna, **kwds)
78367838

78377839
def _get_data(axis_matters):
7838-
if filter_type is None or filter_type == "numeric":
7840+
if filter_type is None:
78397841
data = self._get_numeric_data()
78407842
elif filter_type == "bool":
78417843
if axis_matters:
@@ -7882,15 +7884,11 @@ def blk_func(values):
78827884
return out
78837885

78847886
if numeric_only is None:
7885-
values = self.values
7887+
data = self
7888+
values = data.values
78867889
try:
78877890
result = f(values)
78887891

7889-
if filter_type == "bool" and is_object_dtype(values) and axis is None:
7890-
# work around https://github.com/numpy/numpy/issues/10489
7891-
# TODO: combine with hasattr(result, 'dtype') further down
7892-
# hard since we don't have `values` down there.
7893-
result = np.bool_(result)
78947892
except TypeError:
78957893
# e.g. in nanops trying to convert strs to float
78967894

@@ -7916,30 +7914,36 @@ def blk_func(values):
79167914

79177915
# TODO: why doesnt axis matter here?
79187916
data = _get_data(axis_matters=False)
7919-
with np.errstate(all="ignore"):
7920-
result = f(data.values)
79217917
labels = data._get_agg_axis(axis)
7918+
7919+
values = data.values
7920+
with np.errstate(all="ignore"):
7921+
result = f(values)
79227922
else:
79237923
if numeric_only:
79247924
data = _get_data(axis_matters=True)
7925+
labels = data._get_agg_axis(axis)
79257926

79267927
values = data.values
7927-
labels = data._get_agg_axis(axis)
79287928
else:
7929-
values = self.values
7929+
data = self
7930+
values = data.values
79307931
result = f(values)
79317932

7932-
if hasattr(result, "dtype") and is_object_dtype(result.dtype):
7933+
if filter_type == "bool" and is_object_dtype(values) and axis is None:
7934+
# work around https://github.com/numpy/numpy/issues/10489
7935+
# TODO: can we de-duplicate parts of this with the next blocK?
7936+
result = np.bool_(result)
7937+
elif hasattr(result, "dtype") and is_object_dtype(result.dtype):
79337938
try:
7934-
if filter_type is None or filter_type == "numeric":
7939+
if filter_type is None:
79357940
result = result.astype(np.float64)
79367941
elif filter_type == "bool" and notna(result).all():
79377942
result = result.astype(np.bool_)
79387943
except (ValueError, TypeError):
7939-
79407944
# try to coerce to the original dtypes item by item if we can
79417945
if axis == 0:
7942-
result = coerce_to_dtypes(result, self.dtypes)
7946+
result = coerce_to_dtypes(result, data.dtypes)
79437947

79447948
if constructor is not None:
79457949
result = self._constructor_sliced(result, index=labels)

0 commit comments

Comments
 (0)