Skip to content

Commit

Permalink
Merge pull request #117 from gab23r/perf/speed_up_iter_models
Browse files Browse the repository at this point in the history
Speed up `iter_models` (x2-x3 improvement)
  • Loading branch information
thomasaarholt authored Oct 22, 2024
2 parents 7fb2319 + 566d79f commit b9b7e3f
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/patito/polars.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,10 +833,23 @@ def iter_models(

df = self.validate(drop_superfluous_columns=True) if validate_df else self

def _iter_models(_df: DF) -> Iterator[ModelType]:
for idx in range(_df.height):
yield self.model.from_row(_df[idx], validate=validate_model)

def _iter_models_with_validate(
_df: DataFrame[ModelType],
) -> Iterator[ModelType]:
for row in _df.iter_rows(named=True):
yield self.model(**row)

def _iter_models_without_validate(
_df: DataFrame[ModelType],
) -> Iterator[ModelType]:
for row in _df.iter_rows(named=True):
yield self.model.model_construct(**row)

_iter_models = (
_iter_models_with_validate
if validate_model
else _iter_models_without_validate
)
return ModelGenerator(_iter_models(df))

def _pydantic_model(self) -> type[Model]:
Expand Down

0 comments on commit b9b7e3f

Please sign in to comment.