diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 46b213b25df49..d436385ba61ce 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -7606,6 +7606,23 @@ def _reduce( def f(x): return op(x, axis=axis, skipna=skipna, **kwds) + def _get_data(axis_matters): + if filter_type is None or filter_type == "numeric": + data = self._get_numeric_data() + elif filter_type == "bool": + if axis_matters: + # GH#25101, GH#24434 + data = self._get_bool_data() if axis == 0 else self + else: + data = self._get_bool_data() + else: # pragma: no cover + msg = ( + "Generating numeric_only data with filter_type {f}" + "not supported.".format(f=filter_type) + ) + raise NotImplementedError(msg) + return data + if numeric_only is None: values = self.values try: @@ -7616,7 +7633,7 @@ def f(x): # TODO: combine with hasattr(result, 'dtype') further down # hard since we don't have `values` down there. result = np.bool_(result) - except TypeError as err: + except TypeError: # e.g. in nanops trying to convert strs to float # try by-column first @@ -7639,31 +7656,15 @@ def f(x): result = result.iloc[0] return result - if filter_type is None or filter_type == "numeric": - data = self._get_numeric_data() - elif filter_type == "bool": - data = self._get_bool_data() - else: # pragma: no cover - raise NotImplementedError( - "Handling exception with filter_type {f} not" - "implemented.".format(f=filter_type) - ) from err + # TODO: why doesnt axis matter here? + data = _get_data(axis_matters=False) with np.errstate(all="ignore"): result = f(data.values) labels = data._get_agg_axis(axis) else: if numeric_only: - if filter_type is None or filter_type == "numeric": - data = self._get_numeric_data() - elif filter_type == "bool": - # GH 25101, # GH 24434 - data = self._get_bool_data() if axis == 0 else self - else: # pragma: no cover - msg = ( - "Generating numeric_only data with filter_type {f}" - "not supported.".format(f=filter_type) - ) - raise NotImplementedError(msg) + data = _get_data(axis_matters=True) + values = data.values labels = data._get_agg_axis(axis) else: