Skip to content

Validate table rows before modifying collected data - #3861

Merged
quaquel merged 1 commit into
mesa:mainfrom
Shubham-Padkonde:fix/validate-table-row-before-append
Sep 16, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
Shubham-Padkonde:fix/validate-table-row-before-append

Conversation

@Shubham-Padkonde

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

Prevent a rejected table row from corrupting previously collected data.

Bug / Issue

DataCollector.add_table_row currently appends each column as it validates it. If a later column is missing, it raises ValueError after earlier columns have already been extended. The table then has unequal column lengths, and get_table_dataframe fails even after the caller catches the original error and inserts a complete row.

from mesa.datacollection import DataCollector

dc = DataCollector(tables={"events": ["agent_id", "value"]})
dc.add_table_row("events", {"agent_id": 1, "value": 10})
try:
    dc.add_table_row("events", {"agent_id": 2})
except ValueError:
    pass
dc.get_table_dataframe("events")  # Currently raises: All arrays must be of the same length

Implementation

Validate required columns before appending any values. Preserve the existing error type and message, insertion order, and ignore_missing=True behavior. Document that a row missing required columns leaves the table unchanged.

Testing

  • Added six cases covering a missing first, middle, or last column, with both empty and populated tables. Each checks that rejected insertion preserves the DataFrame and that a subsequent complete row succeeds.
  • Before the fix: four of these cases failed; two passed.
  • After the fix: all 47 data-collector tests passed.
  • Broader core suite: 686 passed on Python 3.12.14 (pytest tests --ignore=tests/visualization --ignore=tests/examples).
  • Ruff lint and formatting checks passed on both changed files.
  • Visualization and example suites, other Python versions, and upstream CI have not been run.

Additional Notes

Prepared with Codex assistance. No new dependency or public API is introduced.

@github-actions github-actions Bot added the testing Release notes label label Sep 15, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.3% [-1.1%, +0.4%] 🔵 +1.1% [+0.8%, +1.3%]
BoltzmannWealth large 🔵 +0.4% [-0.1%, +0.9%] 🔵 -3.7% [-5.9%, -1.4%]
Schelling small 🔵 -1.6% [-2.4%, -0.9%] 🔵 -2.1% [-2.9%, -1.2%]
Schelling large 🔵 +1.0% [+0.3%, +1.6%] 🔵 -0.6% [-2.8%, +1.8%]
WolfSheep small 🔵 -2.0% [-2.6%, -1.5%] 🔵 -4.5% [-8.8%, +0.1%]
WolfSheep large 🔵 -2.2% [-3.3%, -0.7%] 🔵 -5.2% [-8.4%, -1.6%]
SugarscapeG1mt small 🔵 -1.2% [-1.8%, -0.7%] 🔵 -1.9% [-3.1%, -0.7%]
SugarscapeG1mt large 🔵 -1.9% [-4.0%, +0.1%] 🔵 -2.9% [-5.0%, -0.5%]
BoidFlockers small 🔵 -1.7% [-2.3%, -1.1%] 🔵 -0.6% [-0.9%, -0.2%]
BoidFlockers large 🔵 -1.0% [-1.6%, -0.4%] 🔵 -0.5% [-0.7%, -0.2%]
MultiLevelAllianceModel small 🟢 -6.8% [-8.3%, -5.4%] 🔵 -1.4% [-2.2%, -0.4%]
MultiLevelAllianceModel large 🔵 -2.9% [-4.0%, -1.9%] 🟢 -6.7% [-8.1%, -5.2%]

@quaquel quaquel added the bug Release notes label label Sep 16, 2026
@quaquel
quaquel merged commit a7502b6 into mesa:main Sep 16, 2026
17 of 18 checks passed
@Shubham-Padkonde

Copy link
Copy Markdown
Contributor Author

Pleasure to contribute, btw can I get chance for GSoc 2027, If possible I will be start contributing from today itself.

EwoutH pushed a commit that referenced this pull request Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants