11from __future__ import annotations
22
33import copy
4+ import warnings
45from functools import partial
56
67import numpy as np
@@ -48,9 +49,12 @@ def generic_aggregate(
4849
4950 group_idx = np .asarray (group_idx , like = array )
5051
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
5458
5559
5660def _normalize_dtype (dtype , array_dtype , fill_value = None ):
@@ -243,11 +247,18 @@ def __repr__(self):
243247 fill_value = 1 ,
244248 final_fill_value = dtypes .NA ,
245249)
250+
251+
252+ def _mean_finalize (sum_ , count ):
253+ with np .errstate (invalid = "ignore" , divide = "ignore" ):
254+ return sum_ / count
255+
256+
246257mean = Aggregation (
247258 "mean" ,
248259 chunk = ("sum" , "nanlen" ),
249260 combine = ("sum" , "sum" ),
250- finalize = lambda sum_ , count : sum_ / count ,
261+ finalize = _mean_finalize ,
251262 fill_value = (0 , 0 ),
252263 dtypes = (None , np .intp ),
253264 final_dtype = np .floating ,
@@ -256,7 +267,7 @@ def __repr__(self):
256267 "nanmean" ,
257268 chunk = ("nansum" , "nanlen" ),
258269 combine = ("sum" , "sum" ),
259- finalize = lambda sum_ , count : sum_ / count ,
270+ finalize = _mean_finalize ,
260271 fill_value = (0 , 0 ),
261272 dtypes = (None , np .intp ),
262273 final_dtype = np .floating ,
@@ -265,7 +276,8 @@ def __repr__(self):
265276
266277# TODO: fix this for complex numbers
267278def _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 )
269281 result [count <= ddof ] = np .nan
270282 return result
271283
@@ -352,6 +364,10 @@ def _zip_index(array_, idx_):
352364 )
353365
354366
367+ def _pick_second (* x ):
368+ return x [1 ]
369+
370+
355371argmax = Aggregation (
356372 "argmax" ,
357373 preprocess = argreduce_preprocess ,
@@ -360,7 +376,7 @@ def _zip_index(array_, idx_):
360376 reduction_type = "argreduce" ,
361377 fill_value = (dtypes .NINF , 0 ),
362378 final_fill_value = - 1 ,
363- finalize = lambda * x : x [ 1 ] ,
379+ finalize = _pick_second ,
364380 dtypes = (None , np .intp ),
365381 final_dtype = np .intp ,
366382)
@@ -373,7 +389,7 @@ def _zip_index(array_, idx_):
373389 reduction_type = "argreduce" ,
374390 fill_value = (dtypes .INF , 0 ),
375391 final_fill_value = - 1 ,
376- finalize = lambda * x : x [ 1 ] ,
392+ finalize = _pick_second ,
377393 dtypes = (None , np .intp ),
378394 final_dtype = np .intp ,
379395)
@@ -386,7 +402,7 @@ def _zip_index(array_, idx_):
386402 reduction_type = "argreduce" ,
387403 fill_value = (dtypes .NINF , - 1 ),
388404 final_fill_value = - 1 ,
389- finalize = lambda * x : x [ 1 ] ,
405+ finalize = _pick_second ,
390406 dtypes = (None , np .intp ),
391407 final_dtype = np .intp ,
392408)
@@ -399,7 +415,7 @@ def _zip_index(array_, idx_):
399415 reduction_type = "argreduce" ,
400416 fill_value = (dtypes .INF , - 1 ),
401417 final_fill_value = - 1 ,
402- finalize = lambda * x : x [ 1 ] ,
418+ finalize = _pick_second ,
403419 dtypes = (None , np .intp ),
404420 final_dtype = np .intp ,
405421)
0 commit comments