import polars as pl
= pl.DataFrame({
tbl "a": [7, 4, 9, 7, 12],
"b": [9, 8, 10, 5, 10]
})
Validate.assert_below_threshold
='warning', i=None, message=None) Validate.assert_below_threshold(level
Raise an AssertionError
if validation steps exceed a specified threshold level.
The assert_below_threshold()
method checks whether validation steps’ failure rates are below a given threshold level ("warning"
, "error"
, or "critical"
). This is particularly useful in automated testing environments where you want to ensure your data quality meets minimum standards before proceeding.
If any validation step exceeds the specified threshold level, an AssertionError
will be raised with details about which steps failed. If the validation has not yet been interrogated, this method will automatically call interrogate()
with default parameters.
Parameters
level :
str
= 'warning'-
The threshold level to check against, which could be any of
"warning"
(the default),"error"
, or"critical"
. AnAssertionError
will be raised if any validation step exceeds this level. i :
int
= None-
Specific validation step number(s) to check. Can be provided as a single integer or a list of integers. If
None
(the default), all steps are checked. message :
str
= None-
Custom error message to use if assertion fails. If
None
, a default message will be generated that lists the specific steps that exceeded the threshold.
Returns
: None
Raises
:
AssertionError
-
If any specified validation step exceeds the given threshold level.
:
ValueError
-
If an invalid threshold level is provided.
Examples
Below are some examples of how to use the assert_below_threshold()
method. First, we’ll create a simple Polars DataFrame with two columns (a
and b
).
Then a validation plan will be created with thresholds (warning=0.1
, error=0.2
, critical=0.3
). After interrogating, we display the validation report table:
import pointblank as pb
= (
validation =tbl, thresholds=(0.1, 0.2, 0.3))
pb.Validate(data="a", value=5) # 1 failing test unit
.col_vals_gt(columns="b", value=10) # 2 failing test units
.col_vals_lt(columns
.interrogate()
)
validation
Using assert_below_threshold(level="warning")
will raise an AssertionError
if any step exceeds the ‘warning’ threshold:
try:
="warning")
validation.assert_below_threshold(levelexcept AssertionError as e:
print(f"Assertion failed: {e}")
Assertion failed: The following steps exceeded the warning threshold level:
Step 1: Expect that values in `a` should be > `5`.
Step 2: Expect that values in `b` should be < `10`.
Check a specific step against the ‘critical’ threshold using the i=
parameter:
="critical", i=1) # Won't raise an error validation.assert_below_threshold(level
As the first step is below the ‘critical’ threshold (it exceeds the ‘warning’ and ‘error’ thresholds), no error is raised and nothing is printed.
We can also provide a custom error message with the message=
parameter. Let’s try that here:
try:
validation.assert_below_threshold(="error",
level="Data quality too low for processing!"
message
)except AssertionError as e:
print(f"Custom error: {e}")
Custom error: Data quality too low for processing!
See Also
warning()
: Get the ‘warning’ status for each validation steperror()
: Get the ‘error’ status for each validation stepcritical()
: Get the ‘critical’ status for each validation stepassert_passing()
: Assert all validations pass completely