Skip to main content

A Python library for cleaning and validating pandas DataFrames.

Project description

cleanframe: Lightweight Dataframe Cleaning & Validation

cleanframe is a lightweight Python library designed to streamline the process of cleaning and validating dataframes using a simple, schema-based approach. Define your data's expected structure and properties, and let cleanframe handle the rest, providing a clean dataframe and a detailed report of any issues found.


Features

  • Schema-based Validation: Define your data's structure, rules, and constraints in a simple Python dictionary.
  • Comprehensive Reporting: Get a detailed report of all validation issues, including which columns or rows were affected.
  • Automatic Cleaning: Automatically drop or fill rows that don't conform to your schema.
  • Lightweight & Simple: Designed for ease of use in notebooks, production pipelines, or general data ingestion tasks.
  • Cross-Validation: Perform complex checks on your data, like ensuring one column is a function of others.

Installation

You can install cleanframe directly from PyPI using pip:

pip install cleanframe

Quick Start

Get started in just a few lines of code. The following example demonstrates how to define a schema, validate a sample dataframe, and inspect the results.

from cleanframe.data import sample_data
from cleanframe import clean_and_validate, Schema
import pandas as pd

1. Load your dataframe

df = sample_data()
print("Original DataFrame shape:", df.shape)

2. Define your schema using column and dataframe rules

column_rules = {
    'transaction_id': {
        'dtype': 'string',
        'regex': r'^TXN_\d{7}$',
        'allow_null': False,
        'drop_if_invalid': True
    },
    'customer_id': {
        'dtype': 'string',
        'regex': r'^CUST_\d{2}$',
        'allow_null': False,
        'drop_if_invalid': True
    },
    'category': {
        'dtype': 'category',
        'allowed_values': ['Patisserie', 'Milk Products', 'Butchers', 'Beverages', 'Food', 'Computers and electric accessories'],
        'allow_null': False,
        'drop_if_invalid': True,
        'fillna': 'Other'
    },
    'price_per_unit': {
        'dtype': 'float',
        'min': 0,
        'max': 10000,
        'allow_null': False,
        'drop_if_invalid': True,
        'fillna': 0
    },
    'quantity': {
        'dtype': 'int',
        'min': 1,
        'max': 1000,
        'allow_null': False,
        'drop_if_invalid': True,
        'fillna': 1
    },
    'total_spent': {
        'dtype': 'float',
        'min': 0,
        'max': 1000000,
        'allow_null': False,
        'drop_if_invalid': True,
        'fillna': 0
    },
    'payment_method': {
        'dtype': 'category',
        'allowed_values': ['Digital Wallet', 'Credit Card', 'Cash'],
        'allow_null': False,
        'drop_if_invalid': True,
        'fillna': 'Other'
    },
    'location': {
        'dtype': 'category',
        'allowed_values': ['Online', 'In-store'],
        'allow_null': False,
        'drop_if_invalid': True,
        'fillna': 'Other'
    },
    'transactoin_date': {
        'dtype': 'datetime',
        'allow_null': False,
        'drop_if_invalid': True,
        'max': pd.Timestamp.today()
    },
    'discount_applied': {
        'dtype': 'boolean',
        'allow_null': False,
        'drop_if_invalid': True
    }
}

dataframe_rule = {
    "min_rows": 1,
    "unique_keys": ["transaction_id"],
    "no_duplicates": True,
    "cross_validations": [{"type": "comparison", "condition": "total_spent == price_per_unit * quantity"}]
}

schema = Schema(rules = column_rules, dataframe_rule = dataframe_rule)

3. Clean and validate your data

cleaned_df, report = clean_and_validate(df, schema)

4. View the report and the cleaned dataframe

print("\nValidation Report:")
print(report)
print("\nCleaned DataFrame shape:", cleaned_df.shape)

Schema Definition

The schema is a Python dictionary that defines the validation rules for your dataframe. It consists of two main parts:

  • column_rules: A dictionary where each key is a column name and the value is a dictionary of rules for that column (e.g., dtype, min, max, regex, allowed_values, allow_null, fillna, and drop_if_invalid).
  • dataframe_rule: A dictionary for rules that apply to the entire dataframe (e.g., min_rows, unique_keys, no_duplicates, cross_validations).

This structure allows for granular control over every aspect of your data's quality.

Contributing

We welcome contributions! If you find a bug or have a suggestion, please open an issue or submit a pull request on our GitHub repository.

License

This project is licensed under the MIT License.

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

cleanframe-0.2.3.tar.gz (222.9 kB view details)

Uploaded Source

Built Distribution

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

cleanframe-0.2.3-py3-none-any.whl (226.2 kB view details)

Uploaded Python 3

File details

Details for the file cleanframe-0.2.3.tar.gz.

File metadata

  • Download URL: cleanframe-0.2.3.tar.gz
  • Upload date:
  • Size: 222.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for cleanframe-0.2.3.tar.gz
Algorithm Hash digest
SHA256 5c18fc6dee5a35ac2992cf98b891f60127e94206352778717b9ee822a9d6253b
MD5 8e498cfef6b76a6212b69d3b041e9012
BLAKE2b-256 4a91ea0505a75e20fddff04f2a523b7df99c1bbfdf49a9249eee0a54f451e7eb

See more details on using hashes here.

File details

Details for the file cleanframe-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: cleanframe-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 226.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for cleanframe-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 aac6924e9e38b2288d1dee20bd5c271571fa15ae23d5956e31b4973ec161b6f2
MD5 72017a27290baf0e1c34a40f2edb4daf
BLAKE2b-256 d792909ea50462afdc1c66dfb5e687080406954631b8850614b3b5fd17f261dd

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