Skip to main content

A lightweight package to extract, document, and validate feature schemas from pandas DataFrames for ML workflows.

Project description

feature_schema

feature_schema is a lightweight Python package that automatically extracts and documents feature metadata from a pandas DataFrame.
It’s designed for machine learning workflows where you need to understand, validate, or dynamically generate user inputs for model features.


Features

  • Extract feature name
  • Auto-detect feature types (int, float, string, bool, datetime)
  • Numeric metadata: min, max, range
  • Categorical metadata: unique values & counts
  • Nullability check: detect if features contain missing values
  • Human-readable docs (__str__) for quick schema inspection
  • Exportable schema to dict / DataFrame for further use

Installation

pip install feature_schema

Usage

1. Create the Schema for a DataFrame

import pandas as pd
from feature_schema import FeatureSchema

# Sample dataset
df = pd.DataFrame({
    "age": [25, 30, 40, 22],
    "salary": [50000.0, 60000.5, 80000.2, 45000.0],
    "city": ["NY", "SF", "LA", "NY"]
})

# Create Feature schema object
fs = FeatureSchema(df)

# Print schema (human readable)
print(fs.to_dict())

Output:

[
    {'column_name': 'age', 'dtype': 'int64', 'type': 'int', 'nullable': np.False_, 'min': 22.0, 'max': 40.0, 'unique_values': 4}, {'column_name': 'salary', 'dtype': 'float64', 'type': 'float', 'nullable': np.False_, 'min': 45000.0, 'max': 80000.2, 'unique_values': 4}, {'column_name': 'city', 'dtype': 'object', 'type': 'string', 'nullable': np.False_, 'unique_values': 3, 'unique_list': ['NY', 'SF', 'LA']}
]

2. Export Schema as Dictionary / DataFrame

# As dictionary
schema_dict = fs.to_dict()
print(schema_dict)

# As Object
schema_df = fs.schema
print(schema_df)

# As DataFrame
schema_df = fs.to_dataframe()
print(schema_df)

3. Save the Model with Feature Schema

model = LinearRegression()
model.fit(X, y)

# Extract feature schema
fs = FeatureSchema(df)

# Bundle model + schema
package = {
    "model": model,
    "schema": fs.to_dict()  }

# Save with pickle
with open("model_with_schema.pkl", "wb") as f:
    pickle.dump(package, f)

print("✅ Model + schema saved!")

4. Use the Pre-trained Model with Schema for Dynamic Feature Input

Load the pickled package (model + schema)

import pickle
import streamlit as st

uploaded_file = st.file_uploader("Upload your trained ML model (.pkl)", type=["pkl","pickle"])

if uploaded_file is not None:
    package = pickle.load(uploaded_file)
    model = package["model"]
    schema = package["schema"]

    st.success("✅ Model loaded successfully!")
    st.subheader("Enter Input Features:")

    feature_values = []

    # Dynamically generate input widgets based on schema
    for feat in schema:
        col_name = feat["column_name"]
        dtype = feat["type"]
        min_val = feat.get("min", 0)   # default 0 if None
        max_val = feat.get("max", 100) # default 100 if None

        # Unique key to avoid Streamlit widget conflicts
        key = f"input_{col_name}"

        # Render input widgets based on feature type
        if dtype == "int":
            val = st.number_input(
                col_name, min_value=int(min_val), max_value=int(max_val),
                value=int(min_val), step=1, key=key
            )
        elif dtype == "float":
            val = st.number_input(
                col_name, min_value=float(min_val), max_value=float(max_val),
                value=float(min_val), key=key
            )
        else:  # string or other types
            val = st.text_input(col_name, key=key)

        feature_values.append(val)

Why Use feature_schema?

  • Eliminate hardcoding of feature names, types, and value ranges.
  • Automatically generate dynamic input forms for Streamlit or validation schemas for FastAPI.
  • Save and bundle schema with ML models for reproducibility and consistency.
  • Instantly document datasets for your team or project.
  • Validate incoming data to prevent type or value mismatches before predictions.

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

feature_schema-0.1.5.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

feature_schema-0.1.5-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file feature_schema-0.1.5.tar.gz.

File metadata

  • Download URL: feature_schema-0.1.5.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for feature_schema-0.1.5.tar.gz
Algorithm Hash digest
SHA256 effaeede7b95964d71d028d89d6ebcaad4ed4738d68046926e26273d7089959a
MD5 2c35e549425a30a61b23dbf927d04d7e
BLAKE2b-256 b565785aa06338cc0d0caee8e67d08914bb38bf97468928bf2b52dc904c88b93

See more details on using hashes here.

File details

Details for the file feature_schema-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: feature_schema-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for feature_schema-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cf09c163b0988949aefdc4e49064fdab648ed5b56c1cca8d399c7768d141f68e
MD5 02b8adf0ba8c6a134de6e5caa6e1d49b
BLAKE2b-256 f3953daa2b8aa6aba1b3d127b0fab507f18bdc21f505d6baacdf524a3d42dd1b

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