Skip to content

ENH: Use Kahan summation to calculate groupby.sum() #38903

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
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Other enhancements
- :func:`pandas.read_sql_query` now accepts a ``dtype`` argument to cast the columnar data from the SQL database based on user input (:issue:`10285`)
- Improved integer type mapping from pandas to SQLAlchemy when using :meth:`DataFrame.to_sql` (:issue:`35076`)
- :func:`to_numeric` now supports downcasting of nullable ``ExtensionDtype`` objects (:issue:`33013`)
- Improve numerical stability for :meth:`DataFrameGroupBy.sum()` and :meth:`SeriesGroupBy.sum()` through using Kahan summation (:issue:`38778`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no brackets after ...GroupBy.sum?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx


.. ---------------------------------------------------------------------------

Expand Down
17 changes: 8 additions & 9 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,12 @@ def _group_add(complexfloating_t[:, :] out,
const int64_t[:] labels,
Py_ssize_t min_count=0):
"""
Only aggregates on axis=0
Only aggregates on axis=0 using Kahan summation
"""
cdef:
Py_ssize_t i, j, N, K, lab, ncounts = len(counts)
complexfloating_t val, count
complexfloating_t[:, :] sumx
complexfloating_t val, count, t, y
complexfloating_t[:, :] sumx, compensation
int64_t[:, :] nobs
Py_ssize_t len_values = len(values), len_labels = len(labels)

Expand All @@ -481,6 +481,7 @@ def _group_add(complexfloating_t[:, :] out,

nobs = np.zeros((<object>out).shape, dtype=np.int64)
sumx = np.zeros_like(out)
compensation = np.zeros_like(out)

N, K = (<object>values).shape

Expand All @@ -497,12 +498,10 @@ def _group_add(complexfloating_t[:, :] out,
# not nan
if val == val:
nobs[lab, j] += 1
if (complexfloating_t is complex64_t or
complexfloating_t is complex128_t):
# clang errors if we use += with these dtypes
sumx[lab, j] = sumx[lab, j] + val
else:
sumx[lab, j] += val
y = val - compensation[lab, j]
t = sumx[lab, j] + y
compensation[lab, j] = t - sumx[lab, j] - y
sumx[lab, j] = t

for i in range(ncounts):
for j in range(K):
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
import pytest

from pandas.compat import IS64
from pandas.errors import PerformanceWarning

import pandas as pd
Expand Down Expand Up @@ -2174,3 +2175,15 @@ def test_groupby_series_with_tuple_name():
expected = Series([2, 4], index=[1, 2], name=("a", "a"))
expected.index.name = ("b", "b")
tm.assert_series_equal(result, expected)


@pytest.mark.xfail(not IS64, reason="GH#38778: fail on 32-bit system")
def test_groupby_numerical_stability_sum():
# GH#38778
data = [1e16, 1e16, 97, 98, -5e15, -5e15, -5e15, -5e15]
df = DataFrame({"group": [1, 2] * 4, "a": data, "b": data})
result = df.groupby("group").sum()
expected = DataFrame(
{"a": [97.0, 98.0], "b": [97.0, 98.0]}, index=Index([1, 2], name="group")
)
tm.assert_frame_equal(result, expected)