@@ -59,6 +59,7 @@ def step(self):
59
59
60
60
from collections .abc import Callable , Collection , Iterable , Iterator , Sequence
61
61
from typing import TYPE_CHECKING
62
+ import uuid
62
63
63
64
import polars as pl
64
65
from polars ._typing import IntoExpr
@@ -121,18 +122,32 @@ def add(
121
122
obj = self ._get_obj (inplace )
122
123
if isinstance (agents , pl .DataFrame ):
123
124
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
125
132
new_agents = agents
126
133
elif isinstance (agents , dict ):
127
134
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
129
136
new_agents = pl .DataFrame (agents )
130
137
else :
131
- if len (agents ) != len (obj ._agents .columns ):
138
+ # exclude unique_id column
139
+ if len (agents ) != len (obj ._agents .columns ) - 1 :
132
140
raise ValueError (
133
141
"Length of data must match the number of columns in the AgentSet if being added as a Collection."
134
142
)
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
+ )
136
151
137
152
if new_agents ["unique_id" ].dtype != pl .Int64 :
138
153
raise TypeError ("unique_id column must be of type int64." )
0 commit comments