import pointblank as pb
import polars as pl
= pl.DataFrame(
tbl
{"a": ["rb-0343", "ra-0232", "ry-0954", "rc-1343"],
"b": ["ra-0628", "ra-583", "rya-0826", "rb-0735"],
}
)
pb.preview(tbl)
Validate.col_vals_regex
Validate.col_vals_regex(
columns,
pattern,=False,
na_pass=None,
pre=None,
thresholds=True,
active )
Validate whether column values match a regular expression pattern.
The col_vals_regex()
validation method checks whether column values in a table correspond to a pattern=
matching expression. This validation will operate over the number of test units that is equal to the number of rows in the table (determined after any pre=
mutation has been applied).
Parameters
columns : str | list[str] | Column | ColumnSelector | ColumnSelectorNarwhals
-
A single column or a list of columns to validate. Can also use
col()
with column selectors to specify one or more columns. If multiple columns are supplied or resolved, there will be a separate validation step generated for each column. pattern : str
-
A regular expression pattern to compare against.
na_pass : bool = False
-
Should any encountered None, NA, or Null values be considered as passing test units? By default, this is
False
. Set toTrue
to pass test units with missing values. pre : Callable | None = None
-
A pre-processing function or lambda to apply to the data table for the validation step.
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
, andnotify
). 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).
Returns
: Validate
-
The
Validate
object with the added validation step.
Examples
For the examples here, we’ll use a simple Polars DataFrame with two string columns (a
and b
). The table is shown below:
Let’s validate that all of the values in column a
match a particular regex pattern. We’ll determine if this validation had any failing test units (there are four test units, one for each row).
= (
validation =tbl)
pb.Validate(data="a", pattern=r"r[a-z]-[0-9]{4}")
.col_vals_regex(columns
.interrogate()
)
validation
STEP | COLUMNS | VALUES | TBL | EVAL | UNITS | PASS | FAIL | W | S | N | EXT | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
#4CA64C | 1 |
|
✓ | 4 | 4 1.00 |
0 0.00 |
— | — | — | — |
Printing the validation
object shows the validation table in an HTML viewing environment. The validation table shows the single entry that corresponds to the validation step created by using col_vals_regex()
. All test units passed, and there are no failing test units.
Now, let’s use the same regex for a validation on column b
.
= (
validation =tbl)
pb.Validate(data="b", pattern=r"r[a-z]-[0-9]{4}")
.col_vals_regex(columns
.interrogate()
)
validation
The validation table reports two failing test units. The specific failing cases are for the string values of rows 1 and 2 in column b
.