Ejemplo n.º 1
0
 def test_to_instances(self):
     t = TypedDfBuilder("T").require("animal", dtype=str).reserve("age", dtype=int).build()
     df: t = t.of(
         [
             pd.Series(dict(animal="goldfish", age=2)),
             pd.Series(dict(animal="goldfish", age=1)),
             pd.Series(dict(animal="gazelle", age=8)),
             pd.Series(dict(animal="pineapple", age=114)),
             pd.Series(dict(animal="anteater", age=11)),
         ]
     )
     dc = t.create_dataclass()
     instances = df.to_dataclass_instances()
     assert instances == [
         dc("goldfish", 2),
         dc("goldfish", 1),
         dc("gazelle", 8),
         dc("pineapple", 114),
         dc("anteater", 11),
     ]
     assert list(sorted(instances)) == [
         dc("anteater", 11),
         dc("gazelle", 8),
         dc("goldfish", 1),
         dc("goldfish", 2),
         dc("pineapple", 114),
     ]
Ejemplo n.º 2
0
 def test_create_any(self):
     t = TypedDfBuilder("T").require("greeting").reserve("bonus").build()
     dc = t.create_dataclass()
     assert dc is not None
     assert issubclass(dc, TypedDfDataclass)
     assert len(dc.get_fields()) == 2
     assert dc.get_fields()[0].name == "greeting"
     assert dc.get_fields()[0].type == Any
     assert dc.get_fields()[1].name == "bonus"
     assert dc.get_fields()[1].type == Optional[Any]
Ejemplo n.º 3
0
 def test_create_empty(self):
     t = TypedDfBuilder("T").build()
     dc = t.create_dataclass()
     assert dc is not None
     assert issubclass(dc, TypedDfDataclass)
     assert dc.get_fields() == []