import polars as pl
= pl.DataFrame(
tbl
{"a": ["apple", "banana", "cherry", "date"],
"b": [1, 6, 3, 5],
}
)
tbl
a | b |
---|---|
str | i64 |
"apple" | 1 |
"banana" | 6 |
"cherry" | 3 |
"date" | 5 |
Validate whether one or more columns exist in the table.
The col_exists()
method checks whether one or more columns exist in the target table. The only requirement is specification of the column names. Each validation step or expectation will operate over a single test unit, which is whether the column exists or not.
columns : str | list[str]
A single column or a list of columns to validate. If multiple columns are supplied, there will be a separate validation step generated for each column.
thresholds : int | float | bool | tuple | dict | Thresholds = None
Failure threshold levels so that the validation step can react accordingly when exceeding the set levels for different states (warn
, stop
, and notify
). This can be created simply as an integer or float denoting the absolute number or fraction of failing test units for the ‘warn’ level. Otherwise, you can use a tuple of 1-3 values, a dictionary of 1-3 entries, or a Thresholds object.
active : bool = True
A boolean value indicating whether the validation step should be active. Using False
will make the validation step inactive (still reporting its presence and keeping indexes for the steps unchanged).
: Validate
The Validate
object with the added validation step.
For the examples here, we’ll use a simple Polars DataFrame with a string columns (a
) and a numeric column (b
). The table is shown below:
import polars as pl
tbl = pl.DataFrame(
{
"a": ["apple", "banana", "cherry", "date"],
"b": [1, 6, 3, 5],
}
)
tbl
a | b |
---|---|
str | i64 |
"apple" | 1 |
"banana" | 6 |
"cherry" | 3 |
"date" | 5 |
Let’s validate that the columns a
and b
actually exist in the table. We’ll determine if this validation had any failing test units (each validation will have a single test unit).
import pointblank as pb
validation = (
pb.Validate(data=tbl)
.col_exists(columns=["a", "b"])
.interrogate()
)
validation
Pointblank Validation | |||||||||||||
2024-12-20|15:08:33 Polars |
|||||||||||||
STEP | COLUMNS | VALUES | TBL | EVAL | UNITS | PASS | FAIL | W | S | N | EXT | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
#4CA64C | 1 |
|
a | — | ✓ | 1 | 1 1.00 |
0 0.00 |
— | — | — | — | |
#4CA64C | 2 |
|
b | — | ✓ | 1 | 1 1.00 |
0 0.00 |
— | — | — | — | |
2024-12-20 15:08:33 UTC< 1 s2024-12-20 15:08:33 UTC |
Printing the validation
object shows the validation table in an HTML viewing environment. The validation table shows two entries (one check per column) generated by the col_exists()
validation step. Both steps passed since both columns provided in columns=
are present in the table.
Now, let’s check for the existence of a different set of columns.
Pointblank Validation | |||||||||||||
2024-12-20|15:08:33 Polars |
|||||||||||||
STEP | COLUMNS | VALUES | TBL | EVAL | UNITS | PASS | FAIL | W | S | N | EXT | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
#4CA64C | 1 |
|
b | — | ✓ | 1 | 1 1.00 |
0 0.00 |
— | — | — | — | |
#4CA64C66 | 2 |
|
c | — | ✓ | 1 | 0 0.00 |
1 1.00 |
— | — | — | — | |
2024-12-20 15:08:33 UTC< 1 s2024-12-20 15:08:33 UTC |
The validation table reports one passing validation step (the check for column b
) and one failing validation step (the check for column c
, which doesn’t exist).