import polars as pl
= pl.DataFrame(
tbl
{"a": [2, 3, 2, 4, 3, 4],
"b": [5, 6, 1, 6, 8, 5],
"c": [9, 8, 8, 7, 7, 8],
}
)
tbl
a | b | c |
---|---|---|
i64 | i64 | i64 |
2 | 5 | 9 |
3 | 6 | 8 |
2 | 1 | 8 |
4 | 6 | 7 |
3 | 8 | 7 |
4 | 5 | 8 |
Validate.col_vals_between(
columns,
left,
right,
inclusive=(True, True),
na_pass=False,
pre=None,
thresholds=None,
active=True,
)
Do column data lie between two specified values or data in other columns?
The col_vals_between()
validation method checks whether column values in a table fall within a range. The range is specified with three arguments: left=
, right=
, and inclusive=
. The left=
and right=
values specify the lower and upper bounds. These bounds can be specified as literal values or as column names provided within col()
. The 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).
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.
left : float | int
The lower bound of the range. Can be a single numeric value or a column name given in col()
.
right : float | int
The upper bound of the range. Can be a single numeric value or a column name given in col()
.
inclusive : tuple[bool, bool] = (True, True)
A tuple of two boolean values indicating whether the comparison should be inclusive. The position of the boolean values correspond to the left=
and right=
values, respectively. By default, both values are True
.
na_pass : bool = False
Should any encountered None, NA, or Null values be considered as passing test units? By default, this is False
. Set to True
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
, 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 three numeric columns (a
, b
, and c
). The table is shown below:
import polars as pl
tbl = pl.DataFrame(
{
"a": [2, 3, 2, 4, 3, 4],
"b": [5, 6, 1, 6, 8, 5],
"c": [9, 8, 8, 7, 7, 8],
}
)
tbl
a | b | c |
---|---|---|
i64 | i64 | i64 |
2 | 5 | 9 |
3 | 6 | 8 |
2 | 1 | 8 |
4 | 6 | 7 |
3 | 8 | 7 |
4 | 5 | 8 |
Let’s validate that values in column a
are all between the fixed boundary values of 1
and 5
. We’ll determine if this validation had any failing test units (there are six test units, one for each row).
import pointblank as pb
validation = (
pb.Validate(data=tbl)
.col_vals_between(columns="a", left=1, right=5)
.interrogate()
)
validation
Pointblank Validation | |||||||||||||
2024-12-20|15:09:22 Polars |
|||||||||||||
STEP | COLUMNS | VALUES | TBL | EVAL | UNITS | PASS | FAIL | W | S | N | EXT | ||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
#4CA64C | 1 |
|
a | [1, 5] | ✓ | 6 | 6 1.00 |
0 0.00 |
— | — | — | — | |
2024-12-20 15:09:22 UTC< 1 s2024-12-20 15:09:22 UTC |
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_between()
. All test units passed, and there are no failing test units.
Aside from checking a column against two literal values representing the lower and upper bounds, we can also provide column names to the left=
and/or right=
arguments (by using the helper function col()
). In this way, we can perform three additional comparison types:
left=column
, right=column
left=literal
, right=column
left=column
, right=literal
For the next example, we’ll use col_vals_between()
to check whether the values in column b
are between than corresponding values in columns a
(lower bound) and c
(upper bound).
The validation table reports two failing test units. The specific failing cases are:
b
is 1
but the bounds are 2
(a
) and 8
(c
).b
is 8
but the bounds are 3
(a
) and 7
(c
).