Skip to content

BUG: groupby sum turning inf+inf and (-inf)+(-inf) into nan #53623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,9 @@ Groupby/resample/rolling
- Bug in :meth:`GroupBy.groups` with a datetime key in conjunction with another key produced incorrect number of group keys (:issue:`51158`)
- Bug in :meth:`GroupBy.quantile` may implicitly sort the result index with ``sort=False`` (:issue:`53009`)
- Bug in :meth:`GroupBy.var` failing to raise ``TypeError`` when called with datetime64, timedelta64 or :class:`PeriodDtype` values (:issue:`52128`, :issue:`53045`)
- Bug in :meth:`SeriresGroupBy.nth` and :meth:`DataFrameGroupBy.nth` after performing column selection when using ``dropna="any"`` or ``dropna="all"`` would not subset columns (:issue:`53518`)
- Bug in :meth:`SeriresGroupBy.nth` and :meth:`DataFrameGroupBy.nth` raised after performing column selection when using ``dropna="any"`` or ``dropna="all"`` resulted in rows being dropped (:issue:`53518`)
- Bug in :meth:`SeriesGroupBy.nth` and :meth:`DataFrameGroupBy.nth` after performing column selection when using ``dropna="any"`` or ``dropna="all"`` would not subset columns (:issue:`53518`)
- Bug in :meth:`SeriesGroupBy.nth` and :meth:`DataFrameGroupBy.nth` raised after performing column selection when using ``dropna="any"`` or ``dropna="all"`` resulted in rows being dropped (:issue:`53518`)
- Bug in :meth:`SeriesGroupBy.sum` and :meth:`DataFrameGroupby.sum` summing ``np.inf + np.inf`` and ``(-np.inf) + (-np.inf)`` to ``np.nan`` (:issue:`53606`)

Reshaping
^^^^^^^^^
Expand Down
7 changes: 7 additions & 0 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,13 @@ def group_sum(
y = val - compensation[lab, j]
t = sumx[lab, j] + y
compensation[lab, j] = t - sumx[lab, j] - y
if compensation[lab, j] != compensation[lab, j]:
# GH#53606
# If val is +/- infinity compensation is NaN
# which would lead to results being NaN instead
# of +/- infinity. We cannot use util.is_nan
# because of no gil
compensation[lab, j] = 0
sumx[lab, j] = t

_check_below_mincount(
Expand Down
27 changes: 27 additions & 0 deletions pandas/tests/groupby/test_libgroupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
group_cumprod,
group_cumsum,
group_mean,
group_sum,
group_var,
)

Expand Down Expand Up @@ -302,3 +303,29 @@ def test_cython_group_mean_Inf_at_begining_and_end():
actual,
expected,
)


@pytest.mark.parametrize(
"values, out",
[
([[np.inf], [np.inf], [np.inf]], [[np.inf], [np.inf]]),
([[np.inf], [np.inf], [-np.inf]], [[np.inf], [np.nan]]),
([[np.inf], [-np.inf], [np.inf]], [[np.inf], [np.nan]]),
([[np.inf], [-np.inf], [-np.inf]], [[np.inf], [-np.inf]]),
],
)
def test_cython_group_sum_Inf_at_begining_and_end(values, out):
# GH #53606
actual = np.array([[np.nan], [np.nan]], dtype="float64")
counts = np.array([0, 0], dtype="int64")
data = np.array(values, dtype="float64")
labels = np.array([0, 1, 1], dtype=np.intp)

group_sum(actual, counts, data, labels, None, is_datetimelike=False)

expected = np.array(out, dtype="float64")

tm.assert_numpy_array_equal(
actual,
expected,
)