patito.DataFrame.cast
- DataFrame.cast(strict=False)
Cast columns to dtypes specified by the associated Patito model.
- Parameters
strict (
bool
) – If set toFalse
, columns which are technically compliant with the specified field type, will not be casted. For example, a column annotated withint
is technically compliant withpl.UInt8
, even ifpl.Int64
is the default dtype associated withint
-annotated fields. Ifstrict
is set toTrue
, the resulting dtypes will be forced to the default dtype associated with each python type.- Returns
A dataframe with columns casted to the correct dtypes.
- Return type
Examples
Create a simple model:
>>> import patito as pt >>> import polars as pl >>> class Product(pt.Model): ... name: str ... cent_price: int = pt.Field(dtype=pl.UInt16) ...
Now we can use this model to cast some simple data:
>>> Product.DataFrame({"name": ["apple"], "cent_price": ["8"]}).cast() shape: (1, 2) ┌───────┬────────────┐ │ name ┆ cent_price │ │ --- ┆ --- │ │ str ┆ u16 │ ╞═══════╪════════════╡ │ apple ┆ 8 │ └───────┴────────────┘