Skip to content

Commit 5358f29

Browse files
committed
fix : [unique_ids should be generated automatically] (projectmesa#105) - using uuid
1 parent dfddd80 commit 5358f29

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

mesa_frames/concrete/polars/agentset.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def step(self):
5959

6060
from collections.abc import Callable, Collection, Iterable, Iterator, Sequence
6161
from typing import TYPE_CHECKING
62+
import uuid
6263

6364
import polars as pl
6465
from polars._typing import IntoExpr
@@ -121,18 +122,32 @@ def add(
121122
obj = self._get_obj(inplace)
122123
if isinstance(agents, pl.DataFrame):
123124
if "unique_id" not in agents.columns:
124-
raise KeyError("DataFrame must have a unique_id column.")
125+
agents = agents.with_columns(
126+
pl.Series(
127+
"unique_id",
128+
[(uuid.uuid4().int % 10**18) for _ in range(agents.height)],
129+
)
130+
)
131+
#mod 10**18 to avoid overflow
125132
new_agents = agents
126133
elif isinstance(agents, dict):
127134
if "unique_id" not in agents:
128-
raise KeyError("Dictionary must have a unique_id key.")
135+
agents["unique_id"] = uuid.uuid4().int % 10**18
129136
new_agents = pl.DataFrame(agents)
130137
else:
131-
if len(agents) != len(obj._agents.columns):
138+
# exclude unique_id column
139+
if len(agents) != len(obj._agents.columns) - 1:
132140
raise ValueError(
133141
"Length of data must match the number of columns in the AgentSet if being added as a Collection."
134142
)
135-
new_agents = pl.DataFrame([agents], schema=obj._agents.schema)
143+
new_agents = pl.DataFrame(
144+
[
145+
{
146+
**dict(zip(obj._agents.columns, agents)),
147+
"unique_id": (uuid.uuid4().int % 10**18),
148+
}
149+
]
150+
)
136151

137152
if new_agents["unique_id"].dtype != pl.Int64:
138153
raise TypeError("unique_id column must be of type int64.")

0 commit comments

Comments
 (0)