Skip to content

Add get_group to GroupBy - #3713

Merged
quaquel merged 3 commits into
mesa:mainfrom
pragnyanramtha:fix-3680-groupby-get-group
May 15, 2026
Merged

quaquel merged 3 commits into
mesa:mainfrom
pragnyanramtha:fix-3680-groupby-get-group

Conversation

@pragnyanramtha

Copy link
Copy Markdown
Contributor

Important

This implements the get_group API discussed in #3680 after maintainer agreement in #3680 (comment) and follow-up confirmation in #3680 (comment).

Pre-PR Checklist

  • I linked an issue/discussion where a maintainer approved this enhancement/feature for implementation.

Approval Link

#3680 (comment)
#3680 (comment)

Summary

Add GroupBy.get_group(name, default=_MISSING) for explicit group lookup.

Motive

GroupBy currently requires users to reach into .groups directly to retrieve a group. Issue #3680 settled on an explicit get_group method instead of making GroupBy look like a full mapping.

Implementation

  • Added a private _MISSING sentinel so callers can pass default=None without it being treated as an omitted default.
  • Added GroupBy.get_group() to return an existing group, return a supplied default for a missing group, or raise KeyError when no default is supplied.
  • Used a membership check before indexing so missing lookups do not mutate defaultdict(list) groups created by result_type="list".

Usage Examples

grouped_agents = agentset.groupby("status")
infected = grouped_agents.get_group("infected")
recovered = grouped_agents.get_group("recovered", default=None)

Additional Notes

Closes #3680

Validation:

  • uv run --no-project --with scipy --with pytest --with numpy --with pandas --with tqdm python -m pytest tests/test_agentset.py -q -> 26 passed
  • ruff check --no-cache mesa/agentset.py tests/test_agentset.py -> passed
  • ruff format --check --no-cache mesa/agentset.py tests/test_agentset.py -> passed
  • git diff --check -> passed

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.2% [-2.6%, -1.8%] 🔵 +1.8% [+1.5%, +2.1%]
BoltzmannWealth large 🔵 -2.7% [-3.5%, -1.8%] 🔵 -4.2% [-5.4%, -2.7%]
Schelling small 🔵 -0.6% [-1.6%, +0.5%] 🔵 +1.8% [+0.8%, +2.9%]
Schelling large 🔵 -2.1% [-2.8%, -1.5%] 🟢 -5.1% [-6.9%, -3.4%]
WolfSheep small 🔵 -1.7% [-2.2%, -1.2%] 🔵 +1.7% [-2.3%, +6.1%]
WolfSheep large 🟢 -4.1% [-4.9%, -3.3%] 🟢 -7.5% [-8.6%, -6.4%]
SugarscapeG1mt small 🔵 -3.6% [-4.3%, -2.8%] 🔵 +0.8% [-1.1%, +2.6%]
SugarscapeG1mt large 🔵 -0.4% [-1.3%, +0.6%] 🔵 -0.9% [-2.7%, +0.9%]
BoidFlockers small 🔵 -0.8% [-1.2%, -0.3%] 🔵 -2.4% [-2.8%, -2.0%]
BoidFlockers large 🔵 -0.6% [-1.1%, -0.1%] 🔵 -1.4% [-1.7%, -1.1%]

@quaquel quaquel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. Looks good to me.

@quaquel quaquel added the enhancement Release notes label label May 14, 2026
@quaquel
quaquel marked this pull request as ready for review May 14, 2026 19:33
@quaquel

quaquel commented May 14, 2026

Copy link
Copy Markdown
Member

was there a reasons this was a draft or can it be merged?

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.6% [-2.0%, -1.3%] 🔵 -2.0% [-2.2%, -1.8%]
BoltzmannWealth large 🔵 -2.1% [-2.6%, -1.7%] 🔵 -5.0% [-7.2%, -2.6%]
Schelling small 🔵 -0.2% [-0.6%, +0.1%] 🔵 +1.3% [+0.5%, +2.1%]
Schelling large 🔵 +1.3% [+0.9%, +1.8%] 🔵 +3.8% [+1.7%, +5.9%]
WolfSheep small 🔵 -0.2% [-0.5%, +0.1%] 🔵 +0.2% [-3.5%, +4.0%]
WolfSheep large 🔵 -1.3% [-2.2%, -0.4%] 🔵 -1.3% [-3.1%, +0.3%]
SugarscapeG1mt small 🔵 -1.1% [-1.4%, -0.8%] 🔵 +1.4% [-0.2%, +3.2%]
SugarscapeG1mt large 🔵 +0.3% [-0.9%, +1.4%] 🔵 +1.6% [-0.1%, +3.1%]
BoidFlockers small 🔵 +0.5% [-0.0%, +1.0%] 🔵 -0.7% [-1.0%, -0.3%]
BoidFlockers large 🔵 -0.5% [-0.9%, -0.0%] 🔵 -0.5% [-0.6%, -0.3%]

Comment thread mesa/agentset.py Resolved
Comment thread mesa/agentset.py Resolved
this reflects the actually agreed upon code
Copilot AI review requested due to automatic review settings May 15, 2026 06:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review is ineligible. To be eligible to request a review, you need a paid Copilot license, or your organization must enable Copilot code review.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread mesa/agentset.py Resolved
Comment thread mesa/agentset.py Resolved
@pragnyanramtha

Copy link
Copy Markdown
Contributor Author

Thanks for catching this. I updated the branch to follow the final try/except KeyError approach and chain the custom KeyError with from err.

I also normalized GroupBy.__init__ to store dict(groups), so get_group() does not accidentally create a missing key when groups comes from a defaultdict(list).

Validation run locally:

  • /tmp/mesa-agentset-venv/bin/python -m pytest tests/test_agentset.py -k "groupby or get_group"
  • /tmp/mesa-agentset-venv/bin/python -m pytest tests/test_agentset.py

@pragnyanramtha

Copy link
Copy Markdown
Contributor Author

This is ready for review/merge from my side now. I updated the branch to the final try/except KeyError implementation, fixed the indentation from the applied suggestion, and copied groups in GroupBy.__init__ so missing lookups cannot mutate a defaultdict(list) before get_group() handles the default/KeyError path. Local tests/test_agentset.py passes on the updated head.

@codebreaker32 codebreaker32 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @pragnyanramtha

@pragnyanramtha

Copy link
Copy Markdown
Contributor Author

@quaquel ready to merge !

@quaquel
quaquel merged commit 62f83fb into mesa:main May 15, 2026
11 checks passed
falloficaruss added a commit to falloficaruss/mesa that referenced this pull request May 16, 2026
Add get_group to GroupBy (mesa#3713)
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
(cherry picked from commit 62f83fb)
@EwoutH EwoutH mentioned this pull request Nov 15, 2026
53 of 65 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improving GroupBy

4 participants