Skip to main content

sklearn-style ML preprocessing with pandas-native syntax (df.ml.scale(), df.ml.encode(), ...)

Project description

dfml

sklearn-style ML preprocessing with pandas-native syntax.

dfml registers a .ml accessor on every pandas DataFrame — the same mechanism pandas itself uses for .str, .dt, and .cat — so common preprocessing steps read like native pandas instead of sklearn boilerplate.

import pandas as pd
import dfml  # noqa: F401  (side-effect import registers the accessor)

df = pd.read_csv("data.csv")
clean = df.ml.impute().ml.encode().ml.scale()

Why

Preprocessing with plain sklearn usually looks like this:

numeric_features = X.select_dtypes(include=["int64", "float64"]).columns
categorical_features = X.select_dtypes(exclude=["int64", "float64"]).columns

numeric_transformer = Pipeline([
    ("imputer", SimpleImputer()),
    ("scaler", StandardScaler()),
])
categorical_transformer = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("onehot", OneHotEncoder()),
])

preprocessor = ColumnTransformer([
    ("num", numeric_transformer, numeric_features),
    ("cat", categorical_transformer, categorical_features),
])

X_transformed = preprocessor.fit_transform(X)  # numpy array, column names gone

With dfml:

clean = df.ml.impute().ml.encode().ml.scale()  # still a DataFrame, columns intact
  • Stays a DataFrame. sklearn transformers return bare numpy arrays and drop your column names. Every dfml method returns a DataFrame.
  • Chainable. .ml.impute().ml.encode().ml.scale() reads top to bottom like a pipeline, without building one.
  • Auto-detects columns. Numeric vs. categorical columns are inferred by default — pass columns=[...] only when you want to override it.
  • Not a replacement. Every method wraps a real sklearn transformer under the hood (accessible via .ml.fitted_), so you can drop back to raw sklearn any time you need something the accessor doesn't cover.

Install

pip install dfml

Usage

Impute missing values

df.ml.impute(strategy="mean")               # numeric columns, default
df.ml.impute(strategy="most_frequent")       # all columns
df.ml.impute(columns=["age"], strategy="median")

Encode categorical columns

df.ml.encode()                               # one-hot, all non-numeric columns
df.ml.encode(columns=["city"], method="label")
df.ml.encode(columns=["city"], drop_first=True)

Scale numeric columns

df.ml.scale()                                # StandardScaler, all numeric columns
df.ml.scale(method="minmax")
df.ml.scale(columns=["income"], method="robust")

Split into train/test

X_train, X_test, y_train, y_test = df.ml.split(target="churn", test_size=0.2)

Chain everything

X_train, X_test, y_train, y_test = (
    df.ml.impute()
      .ml.encode()
      .ml.scale()
      .ml.split(target="churn", test_size=0.2)
)

Inspect what was fitted

scaled = df.ml.scale()
scaled.ml.fitted_["scale"]["transformer"]   # the actual fitted StandardScaler

Development

git clone https://github.com/Parthshewale18/dfml
cd dfml
pip install -e ".[dev]"
pytest

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dfml-0.1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dfml-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file dfml-0.1.0.tar.gz.

File metadata

  • Download URL: dfml-0.1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for dfml-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eaf877371243fad6b36e228740ed6bf00799a080acc8b1dfa83565a51264e46f
MD5 1d4bbdcb51f75f54daf1320c6f9ad940
BLAKE2b-256 150db46673ade80f8d8b2e3e17c8ae498a51c40b34b816412c63c0610f50f725

See more details on using hashes here.

File details

Details for the file dfml-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dfml-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for dfml-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19c034ca8525959b6664ce809c4b3f4fe85ba70ec6b6df65688858108688001d
MD5 22dd2d8e4f133213daa91ef24e657b3a
BLAKE2b-256 dea5745d9af513532a8a23914c032bf840a629c893a9668bde8811e340a344da

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page