Pelage: Defensive analysis for Polars
  • Get started
  • API Reference
  • Examples
  • Coming from dbt
  • Git
  1. Check functions
  2. has_dtypes
  • API Reference
  • Check functions
    • has_columns
    • has_dtypes
    • has_no_nulls
    • has_no_infs
    • unique
    • unique_combination_of_columns
    • accepted_values
    • not_accepted_values
    • accepted_range
    • maintains_relationships
    • column_is_within_n_std
    • custom_check
  • Checks with group_by
    • has_shape
    • at_least_one
    • not_constant
    • not_null_proportion
    • has_mandatory_values
    • mutually_exclusive_ranges
    • is_monotonic
  • Exceptions
    • PolarsAssertError

On this page

  • has_dtypes
    • Parameters
    • Returns
    • Examples
  1. Check functions
  2. has_dtypes

has_dtypes

checks.has_dtypes(data, items)

Check that the columns have the expected types

Parameters

data: PolarsLazyOrDataFrame

Polars DataFrame or LazyFrame containing data to check.

items: Dict[str, PolarsDataType]

A dictionnary of column name with their expected polars data type:

{
    "col_a": pl.String,
    "col_b": pl.Int64,
    "col_c": pl.Float64,
    ...
}

Returns

Type Description
PolarsLazyOrDataFrame The original polars DataFrame or LazyFrame when the check passes

Examples

>>> import polars as pl
>>> from pelage import checks
>>> df = pl.DataFrame({
...     "name": ["Alice", "Bob", "Charlie"],
...     "age": [20, 30, 40],
...     "city": ["New York", "London", "Paris"],
... })
>>> checks.has_dtypes(df, {
...     "name": pl.String,
...     "age": pl.Int64,
...     "city": pl.String,
... })
shape: (3, 3)
┌─────────┬─────┬──────────┐
│ name    ┆ age ┆ city     │
│ ---     ┆ --- ┆ ---      │
│ str     ┆ i64 ┆ str      │
╞═════════╪═════╪══════════╡
│ Alice   ┆ 20  ┆ New York │
│ Bob     ┆ 30  ┆ London   │
│ Charlie ┆ 40  ┆ Paris    │
└─────────┴─────┴──────────┘
>>> checks.has_dtypes(df, {
...     "age": pl.String,
...     "city": pl.Int64,
... })
Traceback (most recent call last):
    ...
pelage.checks.PolarsAssertError: Details
Error with the DataFrame passed to the check function:
--> Some columns don't have the expected type:
column='age', expected_type=String, real_dtype=Int64
column='city', expected_type=Int64, real_dtype=String
has_columns
has_no_nulls