Skip to content

Fix registering an AgentSet that has no agents yet - #206

Open
Jay-Naik2526 wants to merge 3 commits into
mesa:mainfrom
Jay-Naik2526:fix/empty-agentset-registration
Open

Jay-Naik2526 wants to merge 3 commits into
mesa:mainfrom
Jay-Naik2526:fix/empty-agentset-registration

Conversation

@Jay-Naik2526

Copy link
Copy Markdown

Fixes #204.

Problem

A new AgentSet starts with an empty DataFrame that has no columns (see AgentSet.__init__), so it has no unique_id column until agents are added. AgentSetRegistry.add and AgentSetRegistry._recompute_ids read agentset["unique_id"] directly, so registering a set that has no agents yet raised polars.exceptions.ColumnNotFoundError: unable to find column "unique_id":

from mesa_frames import AgentSet, Model

class E(AgentSet):
    def step(self):
        pass

m = Model()
m.sets += E(m)  # ColumnNotFoundError

Change

  • AgentSet.index returns an empty UInt64 series named unique_id when the column does not exist yet.
  • AgentSetRegistry.add and _recompute_ids use agentset.index instead of reading the column directly.

AgentSet.pos is built on index, so it works for an empty set as well.

I kept the existing design (an empty AgentSet has no schema) because of the comment in __init__ about adding agents later. If you would rather give an empty AgentSet a unique_id schema instead, I am happy to rework it that way.

Tests

  • tests/test_agentsetregistry.py::test_add_empty_agentset: registers an empty set, adds a populated set next to it, then removes the populated set while the empty one stays (this exercises _recompute_ids).
  • tests/test_agentset.py::test_index_empty: index of a new AgentSet is an empty UInt64 series named unique_id.

Both new tests fail on main with the ColumnNotFoundError above and pass with this change. With MESA_FRAMES_RUNTIME_TYPECHECKING=1, the full suite gives 146 passed and 1 failed; the failure is tests/test_datacollector.py::TestDataCollector::test_postgress, which needs a running PostgreSQL database and fails the same way on main. ruff check and ruff format --check pass.

Jay-Naik2526 and others added 2 commits November 5, 2026 19:40
A new AgentSet is created with an empty DataFrame that has no columns,
so it has no unique_id column until agents are added. AgentSetRegistry.add
and _recompute_ids read agentset["unique_id"] directly and raised
polars ColumnNotFoundError for such a set.

AgentSet.index now returns an empty UInt64 Series named unique_id when the
column does not exist yet, and the registry uses it instead of indexing the
column. This also fixes AgentSet.pos for an empty set, since it is based on
index.

Adds tests for registering an empty AgentSet (including removing another
set while it remains registered) and for the index of an empty AgentSet.

Fixes mesa#204
@Jay-Naik2526

Copy link
Copy Markdown
Author

A note on the second commit: it comes from the pre-commit.ci autofix bot, not from this change. The pre-commit.ci check currently fails on main too (after the hook update in #205 it flags two list comprehensions in examples/boltzmann_wealth/performance_plot.py and examples/sugarscape_ig/performance_comparison.py), and the bot fixed them on this branch.

The actual change is only the first commit (4 files: agentset.py, agentsetregistry.py and their tests). I am happy to drop the bot commit if you would rather fix the examples in a separate PR.

The ExampleAgentSetA fixture sets a column on an empty AgentSet, which
fails with polars 2.x (ShapeError: can't broadcast Series of length 4 to
length 0). Build the populated set with AgentSet.add instead, so the new
test passes with both polars 1.36.1 and 2.1.0.
@Jay-Naik2526

Copy link
Copy Markdown
Author

Context on the red checks, in case it helps with review:

  • The CI Checks failures are not caused by this change. main has been failing the same way in its scheduled runs since at least Oct 19. CI installs polars==2.1.0 (the repo's uv.lock pins 1.36.1), and with polars 2.x the shared test fixtures such as ExampleAgentSet / ExampleAgentSetA fail in setup with polars.exceptions.ShapeError: can't broadcast Series 'wealth' of length 4 to length 0. With polars 2.1.0 locally, 52 tests error at setup and 9 fail in tests/test_agentset.py and tests/test_agentsetregistry.py alone, all from those fixtures.
  • The two tests added here avoid those fixtures and pass with both polars 1.36.1 and 2.1.0 (I updated the registry test in the last commit for that reason).
  • I have not opened a separate issue for the polars 2.x breakage. If it would be useful, I can open one and look at making the fixtures compatible.

This branch has not been deployed

No deployments
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.

[Bug]: Registering an empty AgentSet in Model.sets raises ColumnNotFoundError for "unique_id"

1 participant