Skip to content

Fix GroupBy aggregate skipna behavior for all-NaN groups #5262

Open

Description

@ajpotts

Fix GroupBy aggregate skipna behavior for all-NaN groups

Summary

Arkouda GroupBy.aggregate(..., skipna=True) does not currently match pandas behavior for groups containing only NaN values.

Specifically:

  • max returns -np.finfo(np.float64).max (sentinel value)
  • min may return NaN

In pandas, both groupby().max(skipna=True) and groupby().min(skipna=True) return NaN for groups with no valid (non-NaN) values.

This discrepancy causes pandas-equivalency tests to fail and forces test-side hacks.

Expected Behavior (pandas-compatible)

For floating-point reductions with skipna=True:

  • Ignore NaNs during reduction
  • If a group has zero non-NaN elements, the result must be NaN

This should apply consistently to:

  • min
  • max
  • (potentially other reductions with identities)

Current Behavior

  • max: NaNs are replaced by a sentinel (-1.7976931348623157e+308) and never restored
  • min: Behavior is inconsistent; sometimes returns NaN

Acceptance Criteria

  • Arkouda GroupBy.aggregate(..., op, skipna=True) matches pandas for min and max
  • All-NaN groups return NaN
  • Pandas equivalency tests pass without test-side NaN rewriting
  • Behavior is consistent across ops

Related Tests

  • tests/pandas/groupby_test.py::TestGroupBy::test_pandas_equivalency_nan

Notes

This fix aligns Arkouda semantics with pandas and NumPy expectations and removes the need for sentinel-based test adjustments.

Minimal Repro Example (pandas vs Arkouda)

Setup

>>> import arkouda as ak
>>> import numpy as np
>>> import pandas as pd

Data: one group is all-NaN

>>> keys = np.array([0, 0, 1, 1])
>>> vals = np.array([1.0, np.nan, np.nan, np.nan], dtype=np.float64)

>>> df = pd.DataFrame({"keys": keys, "vals": vals})
>>> ak_keys = ak.array(keys)
>>> ak_vals = ak.array(vals)
>>> akg = ak.GroupBy(ak_keys)

pandas (default behavior): all-NaN groups return NaN

Note: GroupBy.max / min do not accept a skipna keyword.
NaN-skipping is the default pandas behavior.

>>> df.groupby("keys")["vals"].max().to_numpy()
array([ 1., nan])

>>> df.groupby("keys")["vals"].min().to_numpy()
array([ 1., nan])

Arkouda (skipna=True today): max returns a sentinel for all-NaN groups

>>> ak_out_keys, ak_out_max = akg.aggregate(ak_vals, "max", True)
>>> ak_out_keys.to_ndarray()
array([0, 1])

>>> ak_out_max.to_ndarray()
# Expected: [1.0, nan]
# Actual today: [1.0, -1.7976931348623157e+308]
array([ 1.00000000e+000, -1.79769313e+308])

Expected Arkouda output (pandas-compatible)

  • For skipna=True, both min and max should return NaN when a group has no non-NaN values.

Activity

  1. ajpotts commented on Jan 8, 2026

    @ajpotts
    ContributorAuthor

    Related to: #5050

  2. added
    bug
    on Jan 8, 2026
  3. added a commit that references this issue on Jan 8, 2026
  4. added this to the Pandas Alignment milestone on Feb 24, 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

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions