66
77from pandas ._libs .missing import is_matching_na
88
9+ from pandas .core .dtypes .common import (
10+ is_bool_dtype ,
11+ is_integer_dtype ,
12+ )
13+
914import pandas as pd
15+ import pandas ._testing as tm
1016from pandas .core .arrays .integer import INT_STR_TO_DTYPE
1117from pandas .tests .extension .base .base import BaseExtensionTests
1218
@@ -191,7 +197,12 @@ def test_reductions_2d_axis0(self, data, method):
191197 kwargs ["ddof" ] = 0
192198
193199 try :
194- result = getattr (arr2d , method )(axis = 0 , ** kwargs )
200+ if method == "mean" and hasattr (data , "_mask" ):
201+ # Empty slices produced by the mask cause RuntimeWarnings by numpy
202+ with tm .assert_produces_warning (RuntimeWarning , check_stacklevel = False ):
203+ result = getattr (arr2d , method )(axis = 0 , ** kwargs )
204+ else :
205+ result = getattr (arr2d , method )(axis = 0 , ** kwargs )
195206 except Exception as err :
196207 try :
197208 getattr (data , method )()
@@ -212,7 +223,7 @@ def get_reduction_result_dtype(dtype):
212223 # i.e. dtype.kind == "u"
213224 return INT_STR_TO_DTYPE [np .dtype (np .uint ).name ]
214225
215- if method in ["mean" , " median" , "sum" , "prod" ]:
226+ if method in ["median" , "sum" , "prod" ]:
216227 # std and var are not dtype-preserving
217228 expected = data
218229 if method in ["sum" , "prod" ] and data .dtype .kind in "iub" :
@@ -229,6 +240,10 @@ def get_reduction_result_dtype(dtype):
229240 self .assert_extension_array_equal (result , expected )
230241 elif method == "std" :
231242 self .assert_extension_array_equal (result , data - data )
243+ elif method == "mean" :
244+ if is_integer_dtype (data ) or is_bool_dtype (data ):
245+ data = data .astype ("Float64" )
246+ self .assert_extension_array_equal (result , data )
232247 # punt on method == "var"
233248
234249 @pytest .mark .parametrize ("method" , ["mean" , "median" , "var" , "std" , "sum" , "prod" ])
0 commit comments