Repository navigation
Fix DataCollector.get_agent_vars_dataframe() on empty records - #3945
Merged
quaquel merged 1 commit intoOct 28, 2026
Merged
Conversation
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).
|
Performance benchmarks:
|
quaquel
approved these changes
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
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 withagent_reportersdefined but no agent records recorded (either beforecollect()or whencollect()runs on a model with zero agents),all_recordsis an empty generator fromitertools.chain.from_iterable(). Passing an empty generator topd.DataFrame.from_records()withindex=["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_recordsto a list (list(itertools.chain.from_iterable(...))) prior to passing it topd.DataFrame.from_records(). This mirrors the fix applied toget_agenttype_vars_dataframe()in #3797.Testing
test_get_agent_vars_dataframe_empty_recordsintests/test_datacollector.pycovering:get_agent_vars_dataframe()before anycollect()call.get_agent_vars_dataframe()aftercollect()on a model with zero agents.df.empty,len(df) == 0, column names, and MultiIndex index names["Step", "AgentID"].tests/test_datacollector.py(48/48 passed).ruff checkandruff format.Additional Notes
None.