@@ -149,23 +149,27 @@ def _mean(group_idx, a, size, fill_value, dtype=np.dtype(np.float64)):
149
149
sums .real = np .bincount (group_idx , weights = a .real , minlength = size )
150
150
sums .imag = np .bincount (group_idx , weights = a .imag , minlength = size )
151
151
else :
152
- sums = np .bincount (group_idx , weights = a , minlength = size ).astype (
153
- dtype , copy = False
154
- )
152
+ sums = np .bincount (group_idx , weights = a , minlength = size )
155
153
156
154
with np .errstate (divide = "ignore" , invalid = "ignore" ):
157
- ret = sums . astype ( dtype , copy = False ) / counts
155
+ ret = sums / counts
158
156
if not np .isnan (fill_value ):
159
157
ret [counts == 0 ] = fill_value
160
- return ret
158
+ if iscomplexobj (a ):
159
+ return ret
160
+ else :
161
+ return ret .astype (dtype , copy = False )
161
162
162
163
163
164
def _sum_of_squres (group_idx , a , size , fill_value , dtype = np .dtype (np .float64 )):
164
165
ret = np .bincount (group_idx , weights = a * a , minlength = size )
165
166
if fill_value != 0 :
166
167
counts = np .bincount (group_idx , minlength = size )
167
168
ret [counts == 0 ] = fill_value
168
- return ret
169
+ if iscomplexobj (a ):
170
+ return ret
171
+ else :
172
+ return ret .astype (dtype , copy = False )
169
173
170
174
171
175
def _var (
@@ -176,7 +180,7 @@ def _var(
176
180
counts = np .bincount (group_idx , minlength = size )
177
181
sums = np .bincount (group_idx , weights = a , minlength = size )
178
182
with np .errstate (divide = "ignore" , invalid = "ignore" ):
179
- means = sums . astype ( dtype , copy = False ) / counts
183
+ means = sums / counts
180
184
counts = np .where (counts > ddof , counts - ddof , 0 )
181
185
ret = (
182
186
np .bincount (group_idx , (a - means [group_idx ]) ** 2 , minlength = size ) / counts
@@ -185,7 +189,10 @@ def _var(
185
189
ret = np .sqrt (ret ) # this is now std not var
186
190
if not np .isnan (fill_value ):
187
191
ret [counts == 0 ] = fill_value
188
- return ret
192
+ if iscomplexobj (a ):
193
+ return ret
194
+ else :
195
+ return ret .astype (dtype , copy = False )
189
196
190
197
191
198
def _std (group_idx , a , size , fill_value , dtype = np .dtype (np .float64 ), ddof = 0 ):
@@ -252,7 +259,10 @@ def _cumsum(group_idx, a, size, fill_value=None, dtype=None):
252
259
253
260
increasing = np .arange (len (a ), dtype = int )
254
261
group_starts = _min (group_idx_srt , increasing , size , fill_value = 0 )[group_idx_srt ]
255
- a_srt_cumsum += - a_srt_cumsum [group_starts ] + a_srt [group_starts ]
262
+ # First subtract large numbers
263
+ a_srt_cumsum -= a_srt_cumsum [group_starts ]
264
+ # Then add potentially small numbers
265
+ a_srt_cumsum += a_srt [group_starts ]
256
266
return a_srt_cumsum [invsortidx ]
257
267
258
268
0 commit comments