@@ -7808,6 +7808,8 @@ def _reduce(
7808
7808
self , op , name , axis = 0 , skipna = True , numeric_only = None , filter_type = None , ** kwds
7809
7809
):
7810
7810
7811
+ assert filter_type is None or filter_type == "bool" , filter_type
7812
+
7811
7813
dtype_is_dt = self .dtypes .apply (
7812
7814
lambda x : is_datetime64_any_dtype (x ) or is_period_dtype (x )
7813
7815
)
@@ -7835,7 +7837,7 @@ def f(x):
7835
7837
return op (x , axis = axis , skipna = skipna , ** kwds )
7836
7838
7837
7839
def _get_data (axis_matters ):
7838
- if filter_type is None or filter_type == "numeric" :
7840
+ if filter_type is None :
7839
7841
data = self ._get_numeric_data ()
7840
7842
elif filter_type == "bool" :
7841
7843
if axis_matters :
@@ -7882,15 +7884,11 @@ def blk_func(values):
7882
7884
return out
7883
7885
7884
7886
if numeric_only is None :
7885
- values = self .values
7887
+ data = self
7888
+ values = data .values
7886
7889
try :
7887
7890
result = f (values )
7888
7891
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 )
7894
7892
except TypeError :
7895
7893
# e.g. in nanops trying to convert strs to float
7896
7894
@@ -7916,30 +7914,36 @@ def blk_func(values):
7916
7914
7917
7915
# TODO: why doesnt axis matter here?
7918
7916
data = _get_data (axis_matters = False )
7919
- with np .errstate (all = "ignore" ):
7920
- result = f (data .values )
7921
7917
labels = data ._get_agg_axis (axis )
7918
+
7919
+ values = data .values
7920
+ with np .errstate (all = "ignore" ):
7921
+ result = f (values )
7922
7922
else :
7923
7923
if numeric_only :
7924
7924
data = _get_data (axis_matters = True )
7925
+ labels = data ._get_agg_axis (axis )
7925
7926
7926
7927
values = data .values
7927
- labels = data ._get_agg_axis (axis )
7928
7928
else :
7929
- values = self .values
7929
+ data = self
7930
+ values = data .values
7930
7931
result = f (values )
7931
7932
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 ):
7933
7938
try :
7934
- if filter_type is None or filter_type == "numeric" :
7939
+ if filter_type is None :
7935
7940
result = result .astype (np .float64 )
7936
7941
elif filter_type == "bool" and notna (result ).all ():
7937
7942
result = result .astype (np .bool_ )
7938
7943
except (ValueError , TypeError ):
7939
-
7940
7944
# try to coerce to the original dtypes item by item if we can
7941
7945
if axis == 0 :
7942
- result = coerce_to_dtypes (result , self .dtypes )
7946
+ result = coerce_to_dtypes (result , data .dtypes )
7943
7947
7944
7948
if constructor is not None :
7945
7949
result = self ._constructor_sliced (result , index = labels )
0 commit comments