Skip to content

Fix DataCollector.get_agent_vars_dataframe() on empty records - #3945

Merged
quaquel merged 1 commit into
mesa:mainfrom
Gracy769:fix-datacollector-empty-agent-records
Oct 28, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
Gracy769:fix-datacollector-empty-agent-records

Conversation

@Gracy769

Copy link
Copy Markdown
Contributor

Note

Use this template for bug fixes only. For enhancements/new features, use the feature template and get maintainer approval in an issue/discussion before opening a PR.

Pre-PR Checklist

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

Summary

Fixes DataCollector.get_agent_vars_dataframe() producing a non-empty DataFrame of length 2 with spurious NaN rows when no agent records exist.

Bug / Issue

Fixes #3944.

When get_agent_vars_dataframe() is called with agent_reporters defined but no agent records recorded (either before collect() or when collect() runs on a model with zero agents), all_records is an empty generator from itertools.chain.from_iterable(). Passing an empty generator to pd.DataFrame.from_records() with index=["Step", "AgentID"] causes pandas to misparse the index parameter as row entries, returning a DataFrame with shape (2, len(reporters) + 2) filled with NaNs instead of an empty DataFrame with shape (0, len(reporters)).

Implementation

Materialize all_records to a list (list(itertools.chain.from_iterable(...))) prior to passing it to pd.DataFrame.from_records(). This mirrors the fix applied to get_agenttype_vars_dataframe() in #3797.

Testing

  • Added unit test test_get_agent_vars_dataframe_empty_records in tests/test_datacollector.py covering:
    • Calling get_agent_vars_dataframe() before any collect() call.
    • Calling get_agent_vars_dataframe() after collect() on a model with zero agents.
    • Asserting df.empty, len(df) == 0, column names, and MultiIndex index names ["Step", "AgentID"].
  • Verified test suite passes in tests/test_datacollector.py (48/48 passed).
  • Verified style conformance with ruff check and ruff format.

Additional Notes

None.

When no agent data has been recorded (e.g. before collect() is called or when collect() runs on a model with zero agents), all_records is an empty generator. Passing an empty generator to pd.DataFrame.from_records with an index parameter causes pandas to misparse the index labels as row entries, producing a non-empty DataFrame of length 2 containing spurious all-NaN rows instead of an empty DataFrame of length 0.

Materialize all_records to a list before constructing the DataFrame, matching the behavior and fix established in get_agenttype_vars_dataframe (mesa#3797).
Copilot AI balanced review requested due to automatic review settings October 16, 2026 09:48

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 has reached their quota limit.

@github-actions github-actions Bot added the testing Release notes label label Oct 16, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +1.5% [+0.4%, +2.6%] 🔵 -0.8% [-1.1%, -0.5%]
BoltzmannWealth large 🔵 -2.4% [-3.3%, -1.4%] 🟢 -11.0% [-14.4%, -7.0%]
Schelling small 🔵 -3.6% [-4.3%, -2.9%] 🔵 +0.7% [-0.3%, +1.5%]
Schelling large 🔵 +0.5% [-0.5%, +1.7%] 🔵 -1.3% [-2.9%, +0.2%]
WolfSheep small 🔵 -3.0% [-3.6%, -2.5%] 🔵 -0.1% [-4.1%, +4.1%]
WolfSheep large 🔵 -1.6% [-3.4%, +0.1%] 🔵 -4.6% [-8.5%, -0.7%]
SugarscapeG1mt small 🔵 +0.8% [+0.3%, +1.3%] 🔵 +2.1% [+0.4%, +3.7%]
SugarscapeG1mt large 🔵 -0.6% [-2.4%, +1.7%] 🔵 +2.0% [+0.0%, +4.1%]
BoidFlockers small 🔵 +0.9% [+0.0%, +1.7%] 🔵 +1.3% [+0.9%, +1.7%]
BoidFlockers large 🔵 +0.5% [-0.2%, +1.2%] 🔵 +0.8% [+0.4%, +1.2%]
MultiLevelAllianceModel small 🟢 -5.3% [-7.5%, -3.3%] 🔵 +0.1% [-0.3%, +0.5%]
MultiLevelAllianceModel large 🟢 -7.0% [-9.6%, -3.8%] 🟢 -8.7% [-10.4%, -6.8%]

@quaquel quaquel added bug Release notes label and removed testing Release notes label labels Oct 28, 2026
@quaquel
quaquel merged commit c45fbb7 into mesa:main Oct 28, 2026
16 of 18 checks passed
@github-actions github-actions Bot added the testing Release notes label label Oct 28, 2026
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
When no agent data has been recorded (e.g. before collect() is called or when collect() runs on a model with zero agents), all_records is an empty generator. Passing an empty generator to pd.DataFrame.from_records with an index parameter causes pandas to misparse the index labels as row entries, producing a non-empty DataFrame of length 2 containing spurious all-NaN rows instead of an empty DataFrame of length 0.

Materialize all_records to a list before constructing the DataFrame, matching the behavior and fix established in get_agenttype_vars_dataframe (#3797).

Co-authored-by: Gracy769 <[email protected]>
(cherry picked from commit c45fbb7)
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.

DataCollector.get_agent_vars_dataframe() produces non-empty DataFrame with spurious NaN rows when no agents exist

3 participants