Example: IfTrueThenZero
Let's demonstrate adding custom business logic using a demo dataset.
This dataset contains information about various guests staying at a hotel. There is one, complex rule: Rewards members don't pay an amenities fee. That is, if has_rewards=True
, then amenities_fee=0
.

We will write general constraint logic called IfTrueThenZero
, which ensures that if the value of a boolean column is True
then the value in another numerical column must be set to 0
.
Creating the Constraint
Validity Check
The validity check should return a series of True/False
values that determine whether each row is valid.
Let's code the logic up using parameters:
column_names
is the list of columns. We'll assume the first column is the boolean column (True/False
) while the second column is the numerical column (that must be0
if the boolean isTrue
)data
is the full datasetCustom parameters: We won't add any custom parameters for this constraint
Transformations
The transformations must return the full datasets with specific columns transformed. We can modify, delete or add columns as long as we can reverse the transformation later.
For our function, we'll remove the 0 value whenever the boolean is True
. This will allow the machine learning to learn the numerical distribution without these extra 0s.
Reversing the transformation is easier. If the boolean column is True
, we'll simply set the numerical column to 0
.
Putting it together
Finally, we can create our custom class by supplying these functions into the create_custom_constraint
factory method. Since our constraint is similar to FixedIncrements, let's call it FixedIncrementsWithExclusion
.
Download the Example
The Python file below includes the code above. You can download the file to inspect and test your custom constraint class.
Using the Constraint
In a separate Python file, you'll create a synthesizer and add the constraints to it. The synthesizer you use will have more information about how to use your custom constraint.
A general example for a single table synthesizer is shown below.
Last updated