fix(transformers): handle DataFrame input in ContinuousOrdinalTransformer - #26
Open
ChrisW09 wants to merge 1 commit into
Open
fix(transformers): handle DataFrame input in ContinuousOrdinalTransformer#26ChrisW09 wants to merge 1 commit into
ChrisW09 wants to merge 1 commit into
Conversation
…rmer
``fit`` iterated ``X.T`` and ``transform`` iterated ``X``. For a DataFrame the
first yields the column labels of the transpose -- the original row index --
and the second yields column names, so a frame produced one mapping per row
and an all-zero result of the wrong shape. Nothing raised:
df = pd.DataFrame({"c1": ["a","b","a","c"], "c2": ["x","y","x","y"]})
ContinuousOrdinalTransformer().fit(df).mapping_ # 4 entries, not 2
...transform(df) # [[0 0] [0 0]]
Normalize to a 2D object array in both methods and index by position. The
Preprocessor path was unaffected -- the SimpleImputer ahead of this step always
hands it an ndarray -- but the transformer is public and documented.
Two related fixes while in here:
- ``transform`` builds its output with ``np.zeros(X.shape, dtype=int)`` instead
of a list comprehension, so zero rows now yield ``(0, n_features)`` rather
than collapsing to a 1-D ``(0,)`` array that breaks a downstream hstack.
- ``transform`` validates the feature count and raises ``PretabDataError``,
matching every other transformer in the package.
Closes #14
Co-Authored-By: Claude Opus 5 (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.
Fixes #14.
Problem
fititeratedX.TandtransformiteratedX. Neither does what it looks like for aDataFrame:
DataFrame.Titerates the column labels of the transpose (the original rowindex), and iterating a DataFrame yields column names. So a frame produced one mapping per
row and an all-zero result of the wrong shape — with no exception:
The
Preprocessorpath was never affected, because theSimpleImputerahead of this stepalways hands it an ndarray. But the transformer is exported from
pretab.transformersanddocumented in the API reference, and direct frame users got plausible-looking garbage.
Fix
Normalize to a 2D object array in both methods (
_as_2d) and index by position.Two related fixes carried in the same change, both in the issue:
transformnow allocatesnp.zeros(X.shape, dtype=int)instead of building a listcomprehension, so zero rows return
(0, n_features)rather than collapsing to a 1-D(0,)array that breaks any downstreamhstack.transformvalidates the feature count and raisesPretabDataError, matching every othertransformer in the package (which get this from
validate_2d_allow_nan).Result
DataFrame, ndarray and plain-list input now all agree.
Tests
New file
tests/test_continuous_ordinal_transformer.py(matching the one-file-per-transformerconvention already used by
test_custombin_transformer.pyand friends), 7 tests: per-columnmappings, frame codes, frame/ndarray equivalence, list input, the empty-input shape, the
feature-count guard, and a check that unknown categories still map to
0.Full suite: 487 passed, 9 xfailed.
ruff checkclean on changed files.On pyright: the count goes 71 → 72, but the extra entry is
Import "pytest" could not be resolvedin the new test file. Pyright is configured against a.venvthat does not existin this checkout, so every test file already produces one of these (34 of them on
main).No new diagnostic in
pretab/itself.🤖 Generated with Claude Code