11
11
12
12
@tg .typechecked
13
13
class ExampleAgentSetPandas (AgentSetPandas ):
14
- def __init__ (self , model : ModelDF ):
14
+ def __init__ (self , model : ModelDF , index : pd . Index ):
15
15
super ().__init__ (model )
16
16
self .starting_wealth = pd .Series ([1 , 2 , 3 , 4 ], name = "wealth" )
17
17
@@ -25,7 +25,7 @@ def step(self) -> None:
25
25
@pytest .fixture
26
26
def fix1_AgentSetPandas () -> ExampleAgentSetPandas :
27
27
model = ModelDF ()
28
- agents = ExampleAgentSetPandas (model )
28
+ agents = ExampleAgentSetPandas (model , pd . Index ([ 0 , 1 , 2 , 3 ], name = "unique_id" ) )
29
29
agents .add ({"unique_id" : [0 , 1 , 2 , 3 ]})
30
30
agents ["wealth" ] = agents .starting_wealth
31
31
agents ["age" ] = [10 , 20 , 30 , 40 ]
@@ -36,7 +36,7 @@ def fix1_AgentSetPandas() -> ExampleAgentSetPandas:
36
36
@pytest .fixture
37
37
def fix2_AgentSetPandas () -> ExampleAgentSetPandas :
38
38
model = ModelDF ()
39
- agents = ExampleAgentSetPandas (model )
39
+ agents = ExampleAgentSetPandas (model , pd . Index ([ 4 , 5 , 6 , 7 ], name = "unique_id" ) )
40
40
agents .add ({"unique_id" : [4 , 5 , 6 , 7 ]})
41
41
agents ["wealth" ] = agents .starting_wealth + 10
42
42
agents ["age" ] = [100 , 200 , 300 , 400 ]
@@ -55,7 +55,7 @@ def fix1_AgentSetPandas_with_pos(fix1_AgentSetPandas) -> ExampleAgentSetPandas:
55
55
class Test_AgentSetPandas :
56
56
def test__init__ (self ):
57
57
model = ModelDF ()
58
- agents = ExampleAgentSetPandas (model )
58
+ agents = ExampleAgentSetPandas (model , pd . Index ([ 0 , 1 , 2 , 3 ]) )
59
59
assert agents .model == model
60
60
assert isinstance (agents .agents , pd .DataFrame )
61
61
assert agents .agents .index .name == "unique_id"
@@ -78,14 +78,15 @@ def test_add(
78
78
79
79
# Test with a list (Sequence[Any])
80
80
result = agents .add ([10 , 5 , 10 ], inplace = False )
81
- assert result .agents .index .to_list () == [0 , 1 , 2 , 3 , 10 ]
81
+ assert result .agents .index .to_list () == [0 , 1 , 2 , 3 , 4 ]
82
82
assert result .agents .wealth .to_list () == [1 , 2 , 3 , 4 , 5 ]
83
83
assert result .agents .age .to_list () == [10 , 20 , 30 , 40 , 10 ]
84
84
assert agents .agents .index .name == "unique_id"
85
85
86
86
# Test with a dict[str, Any]
87
87
agents .add ({"unique_id" : [4 , 5 ], "wealth" : [5 , 6 ], "age" : [50 , 60 ]})
88
88
assert agents .agents .wealth .tolist () == [1 , 2 , 3 , 4 , 5 , 6 ]
89
+ assert agents .agents .index .tolist () == [0 , 1 , 2 , 3 , 4 , 5 ]
89
90
assert agents .agents .age .tolist () == [10 , 20 , 30 , 40 , 50 , 60 ]
90
91
assert agents .agents .index .name == "unique_id"
91
92
@@ -286,6 +287,7 @@ def test__add__(
286
287
287
288
# Test with an AgentSetPandas and a dict
288
289
agents3 = agents + {"unique_id" : 10 , "wealth" : 5 }
290
+ assert agents3 .agents .index .tolist () == [0 , 1 , 2 , 3 , 4 ]
289
291
assert agents3 .agents .wealth .tolist () == [1 , 2 , 3 , 4 , 5 ]
290
292
291
293
def test__contains__ (self , fix1_AgentSetPandas : ExampleAgentSetPandas ):
@@ -358,6 +360,7 @@ def test__iadd__(
358
360
# Test with an AgentSetPandas and a dict
359
361
agents = deepcopy (fix1_AgentSetPandas )
360
362
agents += {"unique_id" : 10 , "wealth" : 5 }
363
+ assert agents .agents .index .tolist () == [0 , 1 , 2 , 3 , 4 ]
361
364
assert agents .agents .wealth .tolist () == [1 , 2 , 3 , 4 , 5 ]
362
365
363
366
def test__iter__ (self , fix1_AgentSetPandas : ExampleAgentSetPandas ):
@@ -436,24 +439,25 @@ def test_agents(
436
439
437
440
# Test agents.setter
438
441
agents .agents = agents2 .agents
439
- assert len (agents .active_agents ) == 4
442
+ assert agents .agents .wealth .tolist () == [11 , 12 , 13 , 14 ]
443
+ assert agents .agents .age .tolist () == [100 , 200 , 300 , 400 ]
440
444
441
445
def test_active_agents (self , fix1_AgentSetPandas : ExampleAgentSetPandas ):
442
446
agents = fix1_AgentSetPandas
443
447
444
448
# Test with select
445
449
agents .select (agents ["wealth" ] > 2 , inplace = True )
446
- assert len ( agents .active_agents ) == 2
450
+ assert agents .active_agents . index . tolist ( ) == [ 2 , 3 ]
447
451
448
452
# Test with active_agents.setter
449
453
agents .active_agents = agents .agents .wealth > 2
450
- assert len ( agents .active_agents ) == 2
454
+ assert agents .active_agents . index . to_list ( ) == [ 2 , 3 ]
451
455
452
456
def test_inactive_agents (self , fix1_AgentSetPandas : ExampleAgentSetPandas ):
453
457
agents = fix1_AgentSetPandas
454
458
455
459
agents .select (agents ["wealth" ] > 2 , inplace = True )
456
- assert len ( agents .active_agents ) == 2
460
+ assert agents .inactive_agents . index . to_list ( ) == [ 0 , 1 ]
457
461
458
462
def test_pos (self , fix1_AgentSetPandas_with_pos : ExampleAgentSetPandas ):
459
463
pos = fix1_AgentSetPandas_with_pos .pos
0 commit comments