Polars row-oriented Boolean conversion: should raw 2 become false?
Validate the allowed raw integers and conversion policy, not just the final Boolean dtype.
Define the business domain before conversion semantics
Do not delegate Boolean meaning to an implicit converter. Define the allowed input domain first. If a status code permits only 0 and 1, value 2 can be invalid input rather than either true or false. A policy that treats every nonzero value as true has different semantics. [1, 2, 3]
Different row- and column-construction results
Polars issue 30541 reports row-oriented construction with a Boolean schema mapping some integers other than 1 to false, while column-oriented construction and cast map nonzero integers to true. It distinguishes strict behavior and an existing related issue. Checking only that the final value is Boolean loses the original integer distinction. [1]
Check before discarding the raw value
Retain the raw integer alongside its converted result. Under a 0-and-1-only contract, quarantine other integers before conversion. Under a nonzero-is-true contract, perform that conversion explicitly. Do not mix policies within a run; hand over the approved domain with the input provider. [1, 3]
A passing and failing case for the chosen domain
Define an illustrative business rule allowing only 0 and 1 in raw_flag. A raw_flag of 1 belongs to the approved Boolean input domain. A raw value of 2 lies outside that domain and must be isolated before conversion. Do not normalize 2 as valid merely because it became false. Value 0 is valid too: this is not a requirement that every input equal 1. Another nonzero-is-true policy needs its own domain and conversion expectation. [1, 3]
Truthound checks the pre-conversion column
Truthound Core's InSetValidator can check raw_flag against the approved 0-and-1 set. Supplying only an already converted false cannot reveal whether the source was 0 or 2. The validator does not choose or repair Boolean conversion semantics. Convert admitted raw values under the explicit policy, then compare the result as well. [1, 2, 3]
Application conditions
Preserve raw_flag and define approved_boolean policy for the same input. Do not inspect the original domain using only an already converted false value.
Alternatives
For a nonzero-is-true policy, calculate raw_flag != 0 explicitly.
Limitations
The 0 and 1 policy is a business choice. Do not generalize that every integer-to-Boolean conversion is wrong or inspect only the resulting false values.
Evidence
- Event checked
- Subject checked
- Modified
