Skip to content

Commit 7bfc2be

Browse files
committed
fix: handle non-finite values in Kahan compensation
1 parent fa5b90a commit 7bfc2be

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

pandas/_libs/groupby.pyx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from cython cimport (
55
)
66
from libc.math cimport (
77
NAN,
8+
isfinite,
89
sqrt,
910
)
1011
from libc.stdlib cimport (
@@ -778,9 +779,9 @@ def group_sum(
778779
if not isna_entry:
779780
nobs[lab, j] += 1
780781

781-
if sum_t is object:
782+
if sum_t is object or sum_t is int64_t or sum_t is uint64_t:
782783
# NB: this does not use 'compensation' like the non-object
783-
# track does.
784+
# and non-integer track does.
784785
if nobs[lab, j] == 1:
785786
# i.e. we haven't added anything yet; avoid TypeError
786787
# if e.g. val is a str and sumx[lab, j] is 0
@@ -793,13 +794,29 @@ def group_sum(
793794
y = val - compensation[lab, j]
794795
t = sumx[lab, j] + y
795796
compensation[lab, j] = t - sumx[lab, j] - y
796-
if compensation[lab, j] != compensation[lab, j]:
797-
# GH#53606
797+
798+
# Handle float overflow
799+
if (
800+
sum_t is float32_t or sum_t is float64_t
801+
) and not isfinite(compensation[lab, j]):
802+
# GH#53606; GH#60303
798803
# If val is +/- infinity compensation is NaN
799804
# which would lead to results being NaN instead
800805
# of +/- infinity. We cannot use util.is_nan
801806
# because of no gil
802807
compensation[lab, j] = 0
808+
809+
# Handle complex overflow
810+
if (
811+
sum_t is complex64_t or sum_t is complex128_t
812+
) and not isfinite(compensation[lab, j].real):
813+
compensation[lab, j].real = 0
814+
815+
if (
816+
sum_t is complex64_t or sum_t is complex128_t
817+
) and not isfinite(compensation[lab, j].imag):
818+
compensation[lab, j].imag = 0
819+
803820
sumx[lab, j] = t
804821
elif not skipna:
805822
if uses_mask:

0 commit comments

Comments
 (0)