Schema.cast#
- classmethod Schema.cast(
- df: DataFrame | LazyFrame,
- /,
Cast a data frame to match the schema.
This method removes superfluous columns and casts all schema columns to the correct dtypes. However, it does not introspect the data frame contents.
Hence, this method should be used with care and
validate()should generally be preferred. It is advised to only use this method ifdfis surely known to adhere to the schema.- Returns:
The input data frame, wrapped in a generic version of the input’s data frame type to reflect schema adherence.
Note
If you only require a generic data frame for the type checker, consider using
typing.cast()instead of this method.Attention
For lazy frames, casting is not performed eagerly. This prevents collecting the lazy frame’s schema but also means that a call to
polars.LazyFrame.collect()further down the line might fail because of the cast and/or missing columns.