Skip to content

fix(output): keep numeric dtypes next to passed-through categories - #174

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/173-output-column-dtypes
Open

ChrisW09 wants to merge 1 commit into
mainfrom
fix/173-output-column-dtypes

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #173.

When a block passes raw categories through (categorical_method="none"), every numeric column of the pandas output, the polars output and the dense block dictionary became object. select_dtypes("number") found nothing and a polars mean() returned None. scikit-learn's ColumnTransformer keeps a dtype per column.

Root cause

The ColumnTransformer stacks all blocks into one NumPy array, which is object as soon as one block holds strings. to_dataframe_output (pretab/compose/output.py) wrapped that array as-is, and build_output_dict returned slices of it.

Fix

In pretab/compose/output.py, only when the stacked array is object:

  • to_dataframe_output, pandas container: the frame is passed through infer_objects().
  • to_dataframe_output, polars container: the frame is built column by column from those inferred pandas columns. This needs no pyarrow, like the existing polars boundary.
  • build_output_dict, dense blocks: a new _infer_numeric_block returns an object block whose columns are all numeric as a numeric array. A block that still holds strings stays object.

Numeric stacked arrays (the usual case) and sparse output take the existing path unchanged.

Behaviour changes

  • With passed-through categories, the numeric DataFrame columns and blocks have a numeric dtype (e.g. float64), and the categories stay object (pandas) or String (polars). Values are unchanged.
  • return_array=True still returns the single stacked object array, since a NumPy array has one dtype.

Documentation

  • Docstrings of to_dataframe_output, build_output_dict and the new helper.
  • docs/core_concepts/outputs_and_inspection.md (set_output section): each DataFrame column keeps its own dtype, as with ColumnTransformer, including the dense blocks.

Tests

In tests/integration/test_output_format.py:

  • test_pandas_output_keeps_numeric_dtypes_next_to_passed_through_categories: fails on main, passes here.
  • test_polars_output_keeps_numeric_dtypes_next_to_passed_through_categories: fails on main, passes here (skipped without polars).
  • test_dense_blocks_keep_numeric_dtypes_next_to_passed_through_categories: fails on main, passes here.
  • test_numeric_outputs_keep_their_dtype (default and dtype=np.float32): guard that passes on both.

Reproduction

The script from #173 on this branch:

pandas dtypes: {'num_age': 'float64', 'cat_city': 'object'} | numeric columns: ['num_age']
polars schema: {'num_age': 'Float64', 'cat_city': 'String'} | mean of num_age: 0.5806451612903225
block dtypes: {'num_age': 'float64', 'cat_city': 'object'}
sklearn ColumnTransformer: {'num__age': 'float64', 'cat__city': 'object'}

Checks

  • just test: 4681 passed, 59 skipped, 7 xfailed; coverage 95.72% (gate 90%).
  • Minimum dependencies (CI job: Python 3.11, numpy 1.24.4, pandas 2.0.3, scipy 1.10.1, scikit-learn 1.6.0): 4497 passed, 174 skipped, 7 xfailed; scripts/quickstart.py passes.
  • 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).
  • The change stays inside pretab.compose.output, which already imports pandas; polars is still imported lazily. No import direction changes.

Merge notes

The ColumnTransformer stacks every block into one array, which is object
as soon as one block passes raw categories through. The pandas and
polars outputs wrapped that array as-is and the dense blocks were slices
of it, so every numeric column became object: select_dtypes("number")
found nothing and a polars mean() returned None.

When the stacked array is object, to_dataframe_output now re-infers the
pandas columns (and builds the polars frame from them), and
build_output_dict returns a dense block that holds only numbers with its
numeric dtype. Numeric outputs are unchanged.

Fixes #173

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pandas/polars/block outputs turn numeric columns into object dtype when categories pass through

1 participant