From 566d79f5fbf218dafe1424651d71794b548afb81 Mon Sep 17 00:00:00 2001 From: gabriel Date: Mon, 21 Oct 2024 14:56:07 +0200 Subject: [PATCH] speed up --- src/patito/polars.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/patito/polars.py b/src/patito/polars.py index dddda0e..491c002 100644 --- a/src/patito/polars.py +++ b/src/patito/polars.py @@ -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]: