@@ -99,22 +99,28 @@ def nansum_of_squares(group_idx, array, *, axis=-1, size=None, fill_value=None,
99
99
100
100
101
101
def nanlen (group_idx , array , * args , ** kwargs ):
102
- return sum (group_idx , (~ isnull (array )).astype (int ), * args , ** kwargs )
102
+ if np .issubdtype (array .dtype , bool ):
103
+ array = ~ array
104
+ else :
105
+ array = ~ isnull (array )
106
+ return sum (group_idx , array .view (np .int8 ), * args , ** kwargs )
103
107
104
108
105
109
def mean (group_idx , array , * , axis = - 1 , size = None , fill_value = None , dtype = None ):
106
110
if fill_value is None :
107
111
fill_value = 0
108
112
out = sum (group_idx , array , axis = axis , size = size , dtype = dtype , fill_value = fill_value )
109
113
with np .errstate (invalid = "ignore" , divide = "ignore" ):
110
- out /= nanlen (group_idx , array , size = size , axis = axis , fill_value = 0 )
114
+ out /= nanlen (group_idx , array , size = size , axis = axis , fill_value = 0 , dtype = np . intp )
111
115
return out
112
116
113
117
114
118
def nanmean (group_idx , array , * , axis = - 1 , size = None , fill_value = None , dtype = None ):
115
119
if fill_value is None :
116
120
fill_value = 0
117
- out = nansum (group_idx , array , size = size , axis = axis , dtype = dtype , fill_value = fill_value )
121
+ mask = isnull (array )
122
+ masked = np .where (mask , 0 , array )
123
+ out = sum (group_idx , masked , size = size , axis = axis , dtype = dtype , fill_value = fill_value )
118
124
with np .errstate (invalid = "ignore" , divide = "ignore" ):
119
- out /= nanlen (group_idx , array , size = size , axis = axis , fill_value = 0 )
125
+ out /= nanlen (group_idx , mask , size = size , axis = axis , fill_value = 0 , dtype = np . intp )
120
126
return out
0 commit comments