Skip to content

fix(data_collection): preserve integer dtype for agent_id in numpy datasets (#3871) - #3892

Merged
quaquel merged 1 commit into
mesa:mainfrom
rishiiicreates:fix/numpy-recorder-agent-id-int
Oct 6, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
rishiiicreates:fix/numpy-recorder-agent-id-int

Conversation

@rishiiicreates

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

Ensures agent_id remains an integer column when converting NumpyAgentDataSet tables in DataRecorder and ParquetDataRecorder, preventing upcasting to float64 (1.0, 2.0, 3.0).

Bug / Issue

Fixes #3871.
When recording a NumpyAgentDataSet, the recorders build stored arrays by horizontally stacking agent IDs (int) with float data arrays using np.hstack. This automatically upcasts the combined numpy array to float64. When converted to a pandas DataFrame or serialized via ParquetDataRecorder, agent_id was rendered as float values (1.0, 2.0), which is inconsistent with agent IDs elsewhere across Mesa and inconvenient for joining/indexing.

Implementation

  • Added df["agent_id"] = df["agent_id"].astype(int) in DataRecorder._convert_numpyAgentDataSet() before returning the table DataFrame.
  • Added df["agent_id"] = df["agent_id"].astype(int) in ParquetDataRecorder before converting the DataFrame into buffered dictionary records.

Testing

  • Added regression test test_numpy_agent_dataset_agent_id_dtype in tests/experimental/test_datarecorder.py verifying pd.api.types.is_integer_dtype(df["agent_id"]) and exact integer values for both DataRecorder and ParquetDataRecorder.
  • All 57 tests pass in tests/experimental/test_datarecorder.py.
  • Lint and format checks passed via ruff check and ruff format --check.

Copilot AI balanced review requested due to automatic review settings September 29, 2026 02:37

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 experimental Release notes label testing Release notes label labels Sep 29, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -12.4% [-13.5%, -11.2%] 🟢 -8.7% [-9.3%, -8.0%]
BoltzmannWealth large 🟢 -9.7% [-11.2%, -7.9%] 🟢 -17.6% [-20.2%, -14.1%]
Schelling small 🟢 -10.4% [-11.4%, -9.4%] 🟢 -5.0% [-5.9%, -4.1%]
Schelling large 🟢 -8.1% [-10.1%, -6.5%] 🟢 -9.6% [-12.9%, -6.9%]
WolfSheep small 🟢 -9.4% [-10.2%, -8.6%] 🔵 -1.8% [-5.7%, +2.5%]
WolfSheep large 🔵 -3.7% [-5.9%, -1.1%] 🔵 -1.2% [-4.5%, +2.0%]
SugarscapeG1mt small 🔴 +9.6% [+8.2%, +11.1%] 🔵 +1.7% [-0.5%, +4.1%]
SugarscapeG1mt large 🔵 +1.2% [-1.5%, +3.5%] 🔵 +1.1% [-0.5%, +3.1%]
BoidFlockers small 🔵 -3.4% [-4.3%, -2.7%] 🔵 -0.7% [-1.7%, +0.3%]
BoidFlockers large 🔵 -3.7% [-4.8%, -2.4%] 🔵 -2.2% [-3.4%, -0.7%]
MultiLevelAllianceModel small 🟢 -7.5% [-9.5%, -5.4%] 🔵 -3.4% [-4.4%, -2.4%]
MultiLevelAllianceModel large 🔵 +1.5% [-0.2%, +3.3%] 🔴 +6.2% [+4.3%, +8.5%]

@rishiiicreates

Copy link
Copy Markdown
Contributor Author

hey, just noting: the examples CI job failure (test_model_steps[VirusAntibodyModel]) is a pre-existing issue on main — the latest main build_lint run shows the same failure. the VirusAntibodyModel in mesa-examples passes a sequence of length 3 to an agentset expecting 20 agents. not related to this PR's changes — all actual build, coverage, and pre-commit checks pass. rebasing on latest main now to trigger a re-run.

…tasets

When a NumpyAgentDataSet was converted to a DataFrame or written to Parquet,
stacking agent_ids onto float data upcast agent_ids to float64 (e.g. 1.0, 2.0).
Now DataRecorder and ParquetDataRecorder explicitly preserve agent_id as an integer.

Fixes mesa#3871
@rishiiicreates
rishiiicreates force-pushed the fix/numpy-recorder-agent-id-int branch from 6895373 to a49e026 Compare October 3, 2026 13:40
@quaquel quaquel added the bug Release notes label label Oct 6, 2026
@quaquel
quaquel merged commit 786fcde into mesa:main Oct 6, 2026
15 of 16 checks passed
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
…tasets (#3892)

When a NumpyAgentDataSet was converted to a DataFrame or written to Parquet,
stacking agent_ids onto float data upcast agent_ids to float64 (e.g. 1.0, 2.0).
Now DataRecorder and ParquetDataRecorder explicitly preserve agent_id as an integer.

Fixes #3871

(cherry picked from commit 786fcde)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataRecorder and ParquetDataRecorder store agent_id as float for numpy datasets

3 participants