Skip to content

Commit

Permalink
speed up
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed Oct 21, 2024
1 parent 0c77c78 commit 566d79f
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 566d79f

Please sign in to comment.