1
1
from __future__ import annotations
2
2
3
3
import copy
4
+ import warnings
4
5
from functools import partial
5
6
6
7
import numpy as np
@@ -48,9 +49,12 @@ def generic_aggregate(
48
49
49
50
group_idx = np .asarray (group_idx , like = array )
50
51
51
- return method (
52
- group_idx , array , axis = axis , size = size , fill_value = fill_value , dtype = dtype , ** kwargs
53
- )
52
+ with warnings .catch_warnings ():
53
+ warnings .filterwarnings ("ignore" , r"All-NaN (slice|axis) encountered" )
54
+ result = method (
55
+ group_idx , array , axis = axis , size = size , fill_value = fill_value , dtype = dtype , ** kwargs
56
+ )
57
+ return result
54
58
55
59
56
60
def _normalize_dtype (dtype , array_dtype , fill_value = None ):
@@ -243,11 +247,18 @@ def __repr__(self):
243
247
fill_value = 1 ,
244
248
final_fill_value = dtypes .NA ,
245
249
)
250
+
251
+
252
+ def _mean_finalize (sum_ , count ):
253
+ with np .errstate (invalid = "ignore" , divide = "ignore" ):
254
+ return sum_ / count
255
+
256
+
246
257
mean = Aggregation (
247
258
"mean" ,
248
259
chunk = ("sum" , "nanlen" ),
249
260
combine = ("sum" , "sum" ),
250
- finalize = lambda sum_ , count : sum_ / count ,
261
+ finalize = _mean_finalize ,
251
262
fill_value = (0 , 0 ),
252
263
dtypes = (None , np .intp ),
253
264
final_dtype = np .floating ,
@@ -256,7 +267,7 @@ def __repr__(self):
256
267
"nanmean" ,
257
268
chunk = ("nansum" , "nanlen" ),
258
269
combine = ("sum" , "sum" ),
259
- finalize = lambda sum_ , count : sum_ / count ,
270
+ finalize = _mean_finalize ,
260
271
fill_value = (0 , 0 ),
261
272
dtypes = (None , np .intp ),
262
273
final_dtype = np .floating ,
@@ -265,7 +276,8 @@ def __repr__(self):
265
276
266
277
# TODO: fix this for complex numbers
267
278
def _var_finalize (sumsq , sum_ , count , ddof = 0 ):
268
- result = (sumsq - (sum_ ** 2 / count )) / (count - ddof )
279
+ with np .errstate (invalid = "ignore" , divide = "ignore" ):
280
+ result = (sumsq - (sum_ ** 2 / count )) / (count - ddof )
269
281
result [count <= ddof ] = np .nan
270
282
return result
271
283
@@ -352,6 +364,10 @@ def _zip_index(array_, idx_):
352
364
)
353
365
354
366
367
+ def _pick_second (* x ):
368
+ return x [1 ]
369
+
370
+
355
371
argmax = Aggregation (
356
372
"argmax" ,
357
373
preprocess = argreduce_preprocess ,
@@ -360,7 +376,7 @@ def _zip_index(array_, idx_):
360
376
reduction_type = "argreduce" ,
361
377
fill_value = (dtypes .NINF , 0 ),
362
378
final_fill_value = - 1 ,
363
- finalize = lambda * x : x [ 1 ] ,
379
+ finalize = _pick_second ,
364
380
dtypes = (None , np .intp ),
365
381
final_dtype = np .intp ,
366
382
)
@@ -373,7 +389,7 @@ def _zip_index(array_, idx_):
373
389
reduction_type = "argreduce" ,
374
390
fill_value = (dtypes .INF , 0 ),
375
391
final_fill_value = - 1 ,
376
- finalize = lambda * x : x [ 1 ] ,
392
+ finalize = _pick_second ,
377
393
dtypes = (None , np .intp ),
378
394
final_dtype = np .intp ,
379
395
)
@@ -386,7 +402,7 @@ def _zip_index(array_, idx_):
386
402
reduction_type = "argreduce" ,
387
403
fill_value = (dtypes .NINF , - 1 ),
388
404
final_fill_value = - 1 ,
389
- finalize = lambda * x : x [ 1 ] ,
405
+ finalize = _pick_second ,
390
406
dtypes = (None , np .intp ),
391
407
final_dtype = np .intp ,
392
408
)
@@ -399,7 +415,7 @@ def _zip_index(array_, idx_):
399
415
reduction_type = "argreduce" ,
400
416
fill_value = (dtypes .INF , - 1 ),
401
417
final_fill_value = - 1 ,
402
- finalize = lambda * x : x [ 1 ] ,
418
+ finalize = _pick_second ,
403
419
dtypes = (None , np .intp ),
404
420
final_dtype = np .intp ,
405
421
)
0 commit comments