Add value counts aggregation to select.group_by - #1114
Open
ebhills wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new counts aggregation option to select.group_by so grouped recipes can return per-value frequency distributions (as JSON-safe dictionaries) instead of only scalar aggregates.
Changes:
- Extend
select.group_byto support acountsaggregation that returns{value_as_string: frequency}per group. - Normalize keys for JSON-safety (string conversion, lowercase boolean strings, and a
"null"key for missing values). - Add a unit test validating the new aggregation and per-column output renaming with
auto_rename_columns: false.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
wrangles/recipe_wrangles/select.py |
Adds schema documentation and implements the counts aggregation using value_counts(dropna=False) with JSON-safe key normalization. |
tests/recipes/wrangles/test_select.py |
Adds a test covering counts output structure and column renaming behavior. |
Comment on lines
+325
to
+327
| description: >- | ||
| Return a dictionary containing the count of each distinct value for | ||
| these column(s). Keys are converted to JSON-safe strings. |
Comment on lines
+1953
to
+1962
| dataframe=pd.DataFrame({ | ||
| "Selection": ["Primary", "Primary", "None"], | ||
| "Review": [False, False, True], | ||
| }), | ||
| ) | ||
|
|
||
| assert df.to_dict(orient="records") == [{ | ||
| "Selection Counts": {"Primary": 2, "None": 1}, | ||
| "Review Counts": {"false": 2, "true": 1}, | ||
| }] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
countsaggregation toselect.group_bynulland booleans as lowercasetrueorfalseWhy
Grouped recipes sometimes need the distribution of values within each group rather than only a scalar count or a list of the original values. Without this aggregation, recipes need additional transformations to construct a value-to-frequency mapping.
Impact
Recipes can now request value counts directly:
The resulting
Selection Countsvalue is a JSON-safe dictionary such as{"Primary": 2, "None": 1}.Validation
TestGroupByclass:30 passedgit diff --checkpassedSyntaxWarningwarnings were emitted; no test failuresmainby one commit and two filesWorkflow
This is the first pull request for the feature and targets the
devstaging destination. After staging validation, open a second pull request from the samerecovery/select-group-by-countsbranch intomain. Do not delete the branch after thedevmerge.