Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions tests/recipes/wrangles/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -1940,6 +1940,27 @@ def test_group_by_to_list_multiple_list(self):
list(df.values[1]) == ['b', [4], [8]]
)

def test_group_by_value_counts_as_json_safe_dictionary(self):
df = wrangles.recipe.run(
"""
wrangles:
- select.group_by:
counts:
- Selection: Selection Counts
- Review: Review Counts
auto_rename_columns: false
""",
dataframe=pd.DataFrame({
"Selection": ["Primary", "Primary", "None", None],
"Review": [False, False, True, None],
}),
)

assert df.to_dict(orient="records") == [{
"Selection Counts": {"Primary": 2, "None": 1, "null": 1},
"Review Counts": {"false": 2, "true": 1, "null": 1},
}]
Comment thread
Copilot marked this conversation as resolved.


def test_group_by_where(self):
"""
Expand Down
22 changes: 22 additions & 0 deletions wrangles/recipe_wrangles/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@ def group_by(
- string
- array
description: The count of values for these column(s)
counts:
type:
- string
- array
description: >-
Return a dictionary containing the count of each distinct value for
these column(s). Keys are converted to JSON-safe strings; missing
values use the key "null" and booleans use lowercase "true"/"false".
std:
type:
- string
Expand Down Expand Up @@ -399,6 +407,20 @@ def percentile_(x):
# Add option to group as a list
elif operation == "list":
operation = list
elif operation == "counts":
def counts(values):
output = {}
for value, count in values.value_counts(dropna=False).items():
if _pd.isna(value):
key = "null"
elif isinstance(value, bool):
key = str(value).lower()
else:
key = str(value)
output[key] = int(count)
return output
counts.__name__ = "counts"
operation = counts

if not isinstance(columns, list): columns = [columns]
for column in columns:
Expand Down
Loading