Repository navigation
Conversation
ContinuousOrdinalTransformer sorted the distinct categories with np.unique, which cannot order an int next to a str. A categorical object column such as product codes (101, "A7", 205) therefore made fit, and the default Preprocessor, raise a TypeError. Categories of one type keep np.unique's order. When they cannot be compared, they are ordered by type name and naturally within each type, so the codes stay deterministic. Fixes #177
This branch has not been deployed
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.
Fixes #177.
The default
"int"categorical encoding (ContinuousOrdinalTransformer) raisedTypeError: '<' not supported between instances of 'str' and 'int'on an object column mixing int and str categories, soPreprocessor().fitfailed on such data.Root cause
ContinuousOrdinalTransformer.fit(pretab/encoding/categorical/ordinal.py) numbered the categories in the order ofnp.unique(non_missing), which sorts them, and Python cannot order anintand astr.Fix
New
_sorted_categorieshelper:np.unique's order when the categories can be compared, so every single-type column is numbered exactly as before;reprwithin a group.fituses it in place ofnp.unique. Missing values are still excluded and code0stays reserved for missing or unseen values.Behaviour changes
"int" < "str".SimpleImputer(strategy="most_frequent")before the encoder can still fail on such a column when the most frequent values tie across types. That is an upstream limitation; later versions break the tie. The integration test therefore uses a column with a single most frequent category.Documentation
ContinuousOrdinalTransformerdocstring: the order for mixed types.docs/representations/categorical_encoding.md(integer encoding): codes follow the sorted order, and how mixed-type columns are ordered.Tests
In
tests/encoding/categorical/test_continuous_ordinal_transformer.py:test_continuous_ordinal_numbers_mixed_type_categories: mapping and transform with mixed types, missing and unseen values. Fails onmain, passes here.test_continuous_ordinal_keeps_the_sorted_order_of_one_type: guard that passes on both.test_preprocessor_encodes_a_mixed_type_categorical_column: default-encoding fit plus a JSON spec round trip. Fails onmain, passes here, including under the minimum dependencies.Reproduction
The script from #177 on this branch:
Checks
just test: 4679 passed, 59 skipped, 7 xfailed; coverage 95.72% (gate 90%).scripts/quickstart.pypasses.just docs: strict build succeeds.just check: all pre-commit and pre-push hooks pass (file hygiene, ruff format, ruff lint, prettier, pyright with 0 errors).pretab.encoding.categorical.ordinal; no import direction changes.Merge notes
main. Git merges this branch cleanly with every open per-issue branch (fix(binning): reject edges the soft encoding cannot use #150 to fix(placement): reject a target whose length differs from X #176).