Skip to content

[FEA] Accumulate integral groupby MEAN in its double output type #25005

Open

Description

@PointKernel

Is your feature request related to a problem? Please describe.

For integral inputs, groupby MEAN returns double but accumulates in the int64 SUM type. It reuses the SUM result and divides by the count afterward (hash_compound_agg_finalizer.cu, sort/aggregate.cpp). A group whose sum exceeds the int64 range returns a wrapped mean even though the mean itself fits.

Reduction MEAN doesn't have this problem. op::mean in reduction_operators.cuh casts each element to the double result type before summing. As a result, the same column gives a correct mean from cudf::reduce and a wrapped one from cudf::groupby.

pandas also accumulates groupby MEAN in float64, while its int64 sum wraps:

M, m = np.iinfo(np.int64).max, np.iinfo(np.int64).min
df = pd.DataFrame({"k": [0, 0, 1, 1, 1], "v": np.array([M, M, m, m, m], dtype="int64")})
df.groupby("k").v.sum()   # -2, -9223372036854775808
df.groupby("k").v.mean()  # 9.223372e+18, -9.223372e+18

Reported in #24972.

Describe the solution you'd like

Accumulate groupby MEAN for integral inputs in double, matching reduction MEAN. SUM is unchanged.

Describe alternatives you've considered

Document the current behavior and have callers cast to FLOAT64 or decimal128 before the groupby.

Activity

  1. added
    libcudfAffects libcudf (C++/CUDA) code.
    improvementImprovement / enhancement to an existing function
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    improvementImprovement / enhancement to an existing functionlibcudfAffects libcudf (C++/CUDA) code.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions