Skip to content

Closes #5307: Replace _make_aggop with Explicit Aggregation Method Definitions - #5451

Merged
ajpotts merged 1 commit into
Bears-R-Us:mainfrom
ajpotts:5307_Replace_make_aggop
Mar 3, 2026
Merged

ajpotts merged 1 commit into
Bears-R-Us:mainfrom
ajpotts:5307_Replace_make_aggop

Conversation

@ajpotts

@ajpotts ajpotts commented Mar 2, 2026 •

Copy link
Copy Markdown
Contributor

Refactor GroupBy and Series Aggregation to Explicit Methods

Summary

This PR removes dynamic method generation for aggregation operations in:

  • DataFrameGroupBy
  • DiffAggregate
  • Series

and replaces them with explicitly defined methods.

The goal is to eliminate runtime method injection via decorators and
_make_aggop closures, resulting in:

  • Clearer API surface
  • Improved static analysis and IDE support
  • Easier reasoning about supported operations
  • Reduced metaprogramming complexity

Key Changes

1. Removed Dynamic GroupBy Operator Injection

Deleted:

  • groupby_operators decorator
  • GROUPBY_REDUCTION_TYPES dependency in dataframe.py
  • _make_aggop classmethod in DataFrameGroupBy
  • _make_aggop classmethod in DiffAggregate

Aggregation methods are now explicitly defined on both classes.


2. Introduced _aggregate in DataFrameGroupBy

A shared _aggregate(opname, colnames=None) method now handles
aggregation logic, including:

  • Filtering numeric and bigint columns
  • Excluding groupby key columns
  • Returning correctly indexed DataFrame results

Explicit aggregation methods now include:

sum, count, prod, var, std, mean, median,
min, max, argmin, argmax, nunique,
any, all, xor, first, mode, unique

Special handling is provided for keyword conflicts:

  • or_() → aliased to or
  • and_() → aliased to and

3. Explicit Aggregations in DiffAggregate

Replaced closure-based _make_aggop with:

  • _agg(opname) helper
  • Explicit reduction methods for all supported groupby operations
  • Keyword-safe aliases for or and and

This makes the class fully declarative and avoids dynamic method
construction.


4. Removed aggregation_operators Decorator from Series

Deleted:

  • aggregation_operators decorator
  • _make_aggop classmethod in Series

Explicitly implemented the following methods:

max, min, mean, sum, std, var, argmax, argmin, prod

Each now directly delegates to self.values.<op>().


Behavioral Impact

  • No functional change to supported operations
  • No change to public API names
  • Improved maintainability and readability
  • Eliminates closure-based runtime method injection

Why This Change?

Dynamic method injection made the API surface harder to:

  • Inspect
  • Debug
  • Statistically analyze
  • Navigate in IDEs

Explicit definitions make supported reductions obvious and future
modifications safer.


Testing

Existing groupby and series aggregation tests pass without modification.

No behavioral regressions expected.

Closes #5307: Replace _make_aggop with Explicit Aggregation Method Definitions

@ajpotts
ajpotts requested a review from jaketrookman March 2, 2026 20:57
@ajpotts
ajpotts force-pushed the 5307_Replace_make_aggop branch from b4787c7 to 5d9a8e7 Compare March 2, 2026 21:27
@ajpotts
ajpotts force-pushed the 5307_Replace_make_aggop branch from 5d9a8e7 to ec1054d Compare March 2, 2026 22:06
@ajpotts
ajpotts marked this pull request as ready for review March 3, 2026 12:41
@ajpotts
ajpotts added this pull request to the merge queue Mar 3, 2026
Merged via the queue into Bears-R-Us:main with commit 908a0e9 Mar 3, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace _make_aggop with Explicit Aggregation Method Definitions

2 participants