fix(learning): learn from categorical columns whose categories differ - #410
Merged
Merged
Conversation
fd.learn raised "TypeError: Categoricals can only be compared if 'categories' are the same" whenever a shared column was categorical on both sides with different categories (including the same categories in a different order, or differing orderedness). Root cause: the diff stage (learning/diff.py::_column_diffs) finds changed cells with an elementwise `messy.eq(clean)`. pandas only compares two Categoricals when their categories are identical, and a messy/clean pair almost never shares categories, because repairing the values is exactly what changes them. Every fd.learn path runs this comparison (positional and keyed alignment, privacy="mask"/"none", context=), so they all crashed before classification. Pairs that happened to share categories, or mixed categorical with object, compared fine and already learned the same profile as the object-dtype run. Fix: cells are compared by value, so the diff stage decodes a categorical side to its object values before comparing. That is exactly the object-dtype equivalent. Every later stage already handled categorical values, so after the diff the learned rules, value maps, examples, embedded memory and audit match the object-dtype run. The recorded dtype_changes still use the original dtypes. Non-categorical columns do not go through the new branch, so their output is unchanged.
Contributor
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
FreshData benchmark report —
|
| fixture | n_rows | n_cols | p50 s | p95 s | peak MB | repair % | false-repair % | preserve % | trust | monotonic | export % |
|---|
Authored-code reduction (Metric 6)
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
fd.learncrashed when a column was categorical in both the messy and clean frames but with different categories. This included the same categories in a different order, and ordered vs unordered categoricals. The error wasTypeError: Categoricals can only be compared if 'categories' are the same. Everyfd.learnpath hit it: positional and keyed alignment, both privacy modes, andcontext=. Categorical columns are now compared and learned by their values, and the learned profile is the same as one learned from the object-dtype equivalent.Root cause
The diff stage (
learning/diff.py::_column_diffs) finds changed cells with an elementwisemessy.eq(clean). pandas only compares two Categoricals whose categories are identical. A messy/clean pair rarely has identical categories, because fixing the values is exactly what changes them. The later stages (classify, extract, holdout evaluation) already handled categorical values. Pairs with matching categories, or with a categorical on only one side, did not crash and already matched the object-dtype profile.Fix
_column_diffsnow converts a categorical side to its object values before comparing, which is what the object-dtype column would hold.dtype_changesstill reports the original dtypes, and non-categorical columns are untouched.Tests
New
tests/learning/test_categorical.py(14 of its tests fail without the fix):key=, for:privacy="none", and its masked flags match underprivacy="mask"tmp_path) keepsprofile_id, and replaying withfd.clean(profile=...)on a categorical batch gives the same values as the object-dtype profile on an object batchVerification
ruff check .: cleanmypy src/freshdata: no issuespytest -m "not online and not large", Python 3.12 / pandas 2.3.3: 5660 passed, 14 skipped