Skip to main content

Type Preserving Scaler

CI

type_preserving_scaler provides a small wrapper around scikit-learn's StandardScaler for projects that want predictable output container types.

What it is

The package exposes one class:

from type_preserving_scaler import StandardScalerThatPreservesInputType

StandardScalerThatPreservesInputType behaves like sklearn.preprocessing.StandardScaler, with one added convention:

  • fit on a pandas DataFrame, and transform() / fit_transform() return a pandas DataFrame
  • fit on a NumPy array, and transform() / fit_transform() return a NumPy ndarray

For DataFrame inputs, the tested behavior preserves the output shape, columns, and index.

Why it exists

Plain StandardScaler can return NumPy arrays even when the caller is working with pandas data. That can force downstream code to manually rebuild DataFrames or carry column/index metadata separately.

This package keeps the common "pandas in, pandas out" workflow while retaining the familiar StandardScaler API.

How it works

The class subclasses sklearn.preprocessing.StandardScaler. During fit(), it delegates to the parent scaler, then calls scikit-learn's set_output() API:

  • set_output(transform="pandas") when fit() receives a pandas DataFrame
  • set_output(transform="default") otherwise

The output type is therefore determined by the data passed to fit(), not by each later call to transform().

Installation

pip install type_preserving_scaler

The package requires Python 3.8 or newer and depends on NumPy, pandas, and scikit-learn.

Usage

import pandas as pd
from type_preserving_scaler import StandardScalerThatPreservesInputType

df = pd.DataFrame({"a": [1, 2, 3], "b": [10, 20, 30]})

scaler = StandardScalerThatPreservesInputType()
scaled = scaler.fit_transform(df)

assert isinstance(scaled, pd.DataFrame)
assert scaled.columns.equals(df.columns)
assert scaled.index.equals(df.index)

NumPy inputs keep NumPy outputs:

import numpy as np
from type_preserving_scaler import StandardScalerThatPreservesInputType

array = np.array([[1.0, 2.0], [3.0, 4.0]])
scaled = StandardScalerThatPreservesInputType().fit_transform(array)

assert isinstance(scaled, np.ndarray)

Development

Install the development dependencies and the package in editable mode:

pip install -r requirements_dev.txt
pip install -e .

Common local commands:

make test
make lint
make docs

Limitations

  • This package only wraps StandardScaler; it is not a general adapter for all scikit-learn transformers.
  • The output type is chosen when fit() runs.
  • The installed scikit-learn version must provide the set_output() API used by the implementation.
  • The package preserves the output container type. It does not change StandardScaler's scaling behavior.

License

MIT.

Changelog

0.0.1

  • First release on PyPI.

Release files for type-preserving-scaler 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for type-preserving-scaler 0.0.2
File Size Uploaded
type_preserving_scaler-0.0.2.tar.gz 12.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for type-preserving-scaler 0.0.2
File Interpreter ABI Platform
type_preserving_scaler-0.0.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size:16.7 kB

Release files / type_preserving_scaler-0.0.2.tar.gz

Download URL type_preserving_scaler-0.0.2.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
eb789ecaa90d66b124fab9a7d07fcafb701ed2c75e9ffa9553459c9145ac5a85
BLAKE2b-256 checksum
How to use checksums
f28f3a430a7d247c3a46ddf3f45c20b5a1af7c848639c729c6f55aeca95530bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / type_preserving_scaler-0.0.2-py2.py3-none-any.whl

Download URL type_preserving_scaler-0.0.2-py2.py3-none-any.whl
Size 4.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
cb22e75c452c53042435be1224904f299d6a38ea76ca839c8495d7bffb5b110e
BLAKE2b-256 checksum
How to use checksums
f2bcceeb94cb6f02e53bc1db01c38105d2dbdf585136cc720503a04a3984ee78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page