Handle None/NaN/pd.NA in primitives (#25)#105
Merged
Conversation
Most scalar primitives previously crashed when applied to a pandas
column with blank cells: `Scale(2.0).transform(None)` raised TypeError,
and the same operation on a DataFrame column containing NaN failed mid-
apply. Real CSV data always has missing values, so this was a sharp edge
for every user.
Introduce an `isnull()` helper and a `@handle_null` decorator. Apply the
decorator to the twelve scalar primitives that legitimately operate on
non-null values (Bin, Cast, ConvertDate, ConvertUnits, FormatNumber,
NormalizeText, Offset, Round, Scale, Substitute, Threshold, Truncate);
they now pass `None`, `float('nan')`, and `pd.NA` through unchanged.
EnumToEnum and NormalizeBoolean keep their existing strict/default
semantics since those already model missing values.
List-consuming primitives (Reduce, MapEach) intentionally reject null
elements with a positional error message rather than silently dropping
them, so that a multi-source rule producing a partial input surfaces as
a data-quality issue.
Resolves #25.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the long-standing crash when primitives encounter missing values. Closes #25.
Before:
```python
Scale(2.0).transform(None) # TypeError: unsupported operand type
df["weight_lbs"].apply(Scale(2.0).transform) # crashes mid-apply on any NaN
```
After: nulls pass through untouched.
```python
Scale(2.0).transform(None) # None
Scale(2.0).transform(float("nan")) # nan
Scale(2.0).transform(pd.NA) #
```
Design
Tests
`tests/test_null_handling.py` (46 new tests, all green):
131 tests pass overall (was 85).
Backwards compatibility
Purely additive — non-null inputs behave exactly as before. The behaviour of `EnumToEnum` and `NormalizeBoolean` (which were already null-aware) is unchanged.
Test plan
🤖 Generated with Claude Code