Pelage: Defensive analysis for Polars
  • Get started
  • API Reference
  • Examples
  • Coming from dbt
  • Git
  1. Check functions
  2. custom_check
  • 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

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

custom_check

checks.custom_check(data, expression)

Use custom Polars expression to check the DataFrame, based on .filter().

The expression when used through the dataframe method .filter() should return an empty dataframe. This expression should express the requierement for values that are not wanted in the dataframe. For instance, if a column should not contain the value 4, use the expression pl.col("column") != 4.

Analog to dbt-utils fonction: expression_is_true

Parameters

data: PolarsLazyOrDataFrame

Polars DataFrame or LazyFrame containing data to check.

expression: pl.Expr

Polar Expression that can be passed to the .filter() method. As describe above, use an expression that should keep forbidden values when passed to the filter

Returns

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

Examples

>>> import polars as pl
>>> import pelage as plg
>>> df = pl.DataFrame({"a": [1, 2, 3]})
>>> df.pipe(plg.custom_check, pl.col("a") < 4)
shape: (3, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│ 1   │
│ 2   │
│ 3   │
└─────┘
>>> df.pipe(plg.custom_check, pl.col("a") != 3)
Traceback (most recent call last):
...
pelage.checks.PolarsAssertError: Details
shape: (1, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│ 3   │
└─────┘
Error with the DataFrame passed to the check function:
--> Unexpected data in `Custom Check`: [(col("a")) != (dyn int: 3)]
column_is_within_n_std
has_shape